Skip to content

Instantly share code, notes, and snippets.

@shinriyo
Last active July 24, 2020 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinriyo/868667c629bd22330b2da358f0df6b1a to your computer and use it in GitHub Desktop.
Save shinriyo/868667c629bd22330b2da358f0df6b1a to your computer and use it in GitHub Desktop.
Sync Document for create (AddSearch with Firestore, Functions, TypeScript)
export const userOnCreateForAddSearch = functions.firestore
.document('users/{userId}')
.onCreate(async (snap, context) => {
const axios = require('axios');
const data = snap.data();
const objectID = snap.id;
/*const args = {
data: { objectID: objectID, ...data },
headers: { "Content-Type": "application/json" }
}*/
// https://www.addsearch.com/docs/api/authentication/?fbclid=IwAR1J8yi9dgbonpe4mio5tk3IO5YeHC4PO3P54ijz8YLS0sOnhcXUAKAMYmw
// curl --user 'sitekey:secret-api-key' https://api...
// how to replace --user parameters to axios?
/*const args = {
data: { objectID: objectID, ...data },
auth: {
"sitekey": secretKey,
},
headers: {
// auth: {
// "sitekey": secretKey,
// },
"Content-Type": "application/json",
}
}*/
const args = {
// data: { objectID: objectID, ...data },
// custom_fields is needed? and renamed objectID to id
withCredentials: true,
custom_fields: {
...data
},
/* not here
auth: {
username: siteKey,
password: secretKey,
},
*/
headers: {
"Content-Type": "application/json",
}
}
// not secretKey? siteKey is correct.
// axios.post(`${baseURL}'/v2/indices/${secretKey}/documents/`, args)
// remove ' !!!!
axios.post(`${baseURL}/v2/indices/${siteKey}/documents/`, args, {
// HTTP Basic Auth
auth: {
username: siteKey,
password: secretKey,
}
})
.then((response: any) => {
console.log(response.data)
})
.catch((error: any) => {
console.log(error)
})
.then(function () {
console.log("*** finish ***")
})
});
@shinriyo
Copy link
Author

shinriyo commented Jul 21, 2020

Your Site Key (Index public key)
-> siteKey
and it is also username for HTTP Basic Auth

Your Secret API (Secret key is used in the authentication header)
-> secretKey
and it is also password for HTTP Basic Auth

@shinriyo
Copy link
Author

shinriyo commented Jul 21, 2020

Not work!

        const args = {
            data: { objectID: objectID, ...data },
            headers: {
                auth: {
                    "sitekey": secretKey,
                },
                "Content-Type": "application/json",
            }
        }
        const args = {
            data: { objectID: objectID, ...data },
            auth: {
                "sitekey": secretKey,
            },
            headers: {

                "Content-Type": "application/json",
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment