-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat(js client): improve things (one axios client, keepAlive, baseUrl… #47
Merged
GuillaumeDecMeetsMore
merged 3 commits into
master
from
guillaume/feat/improve-js-client
Sep 4, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
882d164
feat(js client): improve things (one axios client, keepAlive, baseUrl…
GuillaumeDecMeetsMore 55dc607
Merge branch 'master' into guillaume/feat/improve-js-client
GuillaumeDecMeetsMore 0870f49
refactor: avoid destructuring in parameters + improve comments
GuillaumeDecMeetsMore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* Partial credentials to be used for the client | ||
*/ | ||
export type PartialCredentials = { | ||
/** | ||
* API key (admin) | ||
*/ | ||
apiKey?: string | ||
/** | ||
* Nettu account id (admin) | ||
*/ | ||
nettuAccount?: string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nettu or Nettei? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the moment |
||
/** | ||
* Token (user) | ||
*/ | ||
token?: string | ||
} | ||
|
||
/** | ||
* Create credentials for the client (admin or user) | ||
* @param creds partial credentials | ||
* @returns credentials | ||
*/ | ||
export const createCreds = (creds?: PartialCredentials): ICredentials => { | ||
if (creds?.apiKey) { | ||
return new AccountCreds(creds.apiKey) | ||
} | ||
if (creds?.nettuAccount) { | ||
return new UserCreds(creds?.nettuAccount, creds?.token) | ||
} | ||
// throw new Error("No api key or nettu account provided to nettu client."); | ||
return new EmptyCreds() | ||
} | ||
|
||
/** | ||
* Credentials for the API for end users (usually frontend) | ||
*/ | ||
export class UserCreds implements ICredentials { | ||
private readonly nettuAccount: string | ||
private readonly token?: string | ||
|
||
constructor(nettuAccount: string, token?: string) { | ||
this.nettuAccount = nettuAccount | ||
this.token = token | ||
} | ||
|
||
createAuthHeaders() { | ||
const creds: Record<string, string> = { | ||
'nettu-account': this.nettuAccount, | ||
} | ||
if (this.token) { | ||
creds.authorization = `Bearer ${this.token}` | ||
} | ||
|
||
return Object.freeze(creds) | ||
} | ||
} | ||
|
||
/** | ||
* Credentials for the API for admins (usually backend) | ||
*/ | ||
export class AccountCreds implements ICredentials { | ||
private readonly apiKey: string | ||
|
||
constructor(apiKey: string) { | ||
this.apiKey = apiKey | ||
} | ||
|
||
createAuthHeaders() { | ||
return Object.freeze({ | ||
'x-api-key': this.apiKey, | ||
}) | ||
} | ||
} | ||
|
||
export interface ICredentials { | ||
createAuthHeaders(): object | ||
} | ||
|
||
export class EmptyCreds implements ICredentials { | ||
createAuthHeaders() { | ||
return Object.freeze({}) | ||
} | ||
} | ||
|
||
export interface ICredentials { | ||
createAuthHeaders(): object | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you document? What's the difference between this and
createAxiosInstanceBackend
?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.
Updated 👍
The main difference is that one allow
keepAlive
, which needs the function to beasync
as it needs to import a Node package.Technically, it could be only one function, but I think it makes it easier for the frontend to still have a function that isn't async. That way, it can be defined directly as a variable in a file (
export ....
) on the frontend