-
Notifications
You must be signed in to change notification settings - Fork 66
Allows passed API key to be a callback function #861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allows passed API key to be a callback function #861
Conversation
|
Hello @a11rew, PS: This message was sent automatically! |
| // Validate parameters | ||
| if (typeof hostUrl !== 'string') { | ||
| throw new TypeError( | ||
| 'Provided hostUrl value (1st parameter) is not a string, expected string' | ||
| ) | ||
| } | ||
|
|
||
| if (typeof apiKey !== 'string' && typeof apiKey !== 'function') { | ||
| throw new TypeError( | ||
| 'Provided apiKey value (2nd parameter) is not a string or a function, expected string or function' | ||
| ) | ||
| } | ||
|
|
||
| // If apiKey is function, call it to get the apiKey | ||
| if (typeof apiKey === 'function') { | ||
| const apiKeyFnValue = apiKey() | ||
| if (typeof apiKeyFnValue !== 'string') { | ||
| throw new TypeError( | ||
| 'Provided apiKey function (2nd parameter) did not return a string, expected string' | ||
| ) | ||
| } | ||
|
|
||
| // Replace apiKey with the value returned by the function | ||
| apiKey = apiKeyFnValue | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be nice to move this to a validate.ts file to avoid to much code in the client constructor. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that would work, moving lines 37-48 into a separate function in a different file.
I'll be leaving the call that resolves the function (lines 50-61) to a string here however so the validate function isn't returning values, just throwing errors on invalid values. What do you think, is that fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about moving it to an apart function not related to validation?
function getApiKey(apiKey: string | () => string) {
// If apiKey is function, call it to get the apiKey
if (typeof apiKey === 'function') {
const apiKeyFnValue = apiKey()
if (typeof apiKeyFnValue !== 'string') {
throw new TypeError(
'Provided apiKey function (2nd parameter) did not return a string, expected string'
)
}
return apiKeyFnValue
}
return apiKey
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved it out of the constructor, albeit in the same file since it throws an error specific only to the client constructor
bidoubiwa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this amazing PR 🔥
Co-authored-by: cvermand <33010418+bidoubiwa@users.noreply.github.com>
…meilisearch into support-api-key-callback
bidoubiwa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🔥
|
bors merge |
|
Build succeeded: |
|
This message is sent automatically Thanks again for contributing to Meilisearch ❤️ |
Pull Request
Related issue
Fixes #765
What does this PR do?
instantMeiliSearchparametersinstantMeiliSearchparameters validationPR checklist
Please check if your PR fulfills the following requirements:
Thank you so much for contributing to Meilisearch!