Skip to content

Commit

Permalink
Merge pull request #17 from getAlby/task-user-agent
Browse files Browse the repository at this point in the history
feat: add user_agent to options
  • Loading branch information
rolznz authored Jun 7, 2023
2 parents f2733f3 + 340e2b5 commit 3c30704
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/OAuth2User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface OAuth2UserOptions {
callback: string;
scopes: OAuth2Scopes[];
request_options?: Partial<RequestOptions>;
user_agent: string;
token?: Token;
}

Expand Down Expand Up @@ -48,7 +49,7 @@ export class OAuth2User implements OAuthClient {
*/
async refreshAccessToken(): Promise<{ token: Token }> {
const refresh_token = this.token?.refresh_token;
const { client_id, client_secret, request_options } = this.options;
const { client_id, client_secret, request_options, user_agent } = this.options;
if (!client_id) {
throw new Error("client_id is required");
}
Expand All @@ -63,6 +64,7 @@ export class OAuth2User implements OAuthClient {
grant_type: "refresh_token",
refresh_token,
},
user_agent,
method: "POST",
headers: {
...request_options?.headers,
Expand Down Expand Up @@ -91,7 +93,7 @@ export class OAuth2User implements OAuthClient {
* Request an access token
*/
async requestAccessToken(code?: string): Promise<{ token: Token }> {
const { client_id, client_secret, callback, request_options } =
const { client_id, client_secret, callback, request_options, user_agent } =
this.options;
const code_verifier = this.code_verifier;
if (!client_id) {
Expand All @@ -111,6 +113,7 @@ export class OAuth2User implements OAuthClient {
...request_options,
endpoint: `/oauth/token`,
params,
user_agent,
method: "POST",
headers: {
...request_options?.headers,
Expand Down
5 changes: 1 addition & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ export class Client {
this.auth = typeof auth === "string" ? new OAuth2Bearer(auth) : auth;
this.defaultRequestOptions = {
...requestOptions,
headers: {
"User-Agent": "alby-js-api",
...requestOptions?.headers,
},
user_agent: requestOptions?.user_agent
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface RequestOptions extends Omit<RequestInit, "body"> {
auth?: AuthClient;
endpoint: string;
params?: Record<string, any>;
user_agent?: string;
request_body?: Record<string, any>;
method?: string;
max_retries?: number;
Expand Down Expand Up @@ -61,6 +62,7 @@ export async function request({
method,
max_retries,
base_url = BASE_URL,
user_agent,
headers,
...options
}: RequestOptions): Promise<Response> {
Expand All @@ -79,6 +81,10 @@ export async function request({
: undefined),
...authHeader,
...headers,
...{
"User-Agent": user_agent ?? "alby-js-api",
"X-User-Agent": user_agent ?? "alby-js-api"
},
},
method,
body: isPost ? JSON.stringify(request_body) : undefined,
Expand Down

0 comments on commit 3c30704

Please sign in to comment.