Last active
July 24, 2020 09:22
-
-
Save shinriyo/868667c629bd22330b2da358f0df6b1a to your computer and use it in GitHub Desktop.
Sync Document for create (AddSearch with Firestore, Functions, TypeScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ***") | |
}) | |
}); | |
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
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