Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit db2155c

Browse files
authored
Add API version header (#45)
1 parent de1fa2f commit db2155c

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/clients.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Client as BaseUserApi } from "./userapi.ts";
66

77
interface AdminApiOptions {
88
adminKey: string;
9+
apiVersion?: string;
910
baseUrl?: string;
1011
}
1112

@@ -39,6 +40,10 @@ export class AdminApi extends BaseAdminApi {
3940
}
4041

4142
const headers: Headers = new Headers();
43+
headers.set(
44+
constants.API_VERSION_HEADER,
45+
options.apiVersion || constants.API_VERSION,
46+
);
4247
headers.set(constants.AUTH_HEADER, "Bearer " + adminKey);
4348

4449
super(new HttpTransport({ headers, baseUrl: options.baseUrl }));
@@ -48,6 +53,7 @@ export class AdminApi extends BaseAdminApi {
4853
interface UserApiOptions {
4954
userKey: string;
5055
accessToken?: string;
56+
apiVersion?: string;
5157
baseUrl?: string;
5258
}
5359

@@ -103,6 +109,10 @@ export class UserApi extends BaseUserApi {
103109
}
104110

105111
const headers: Headers = new Headers();
112+
headers.set(
113+
constants.API_VERSION_HEADER,
114+
options.apiVersion || constants.API_VERSION,
115+
);
106116
headers.set(constants.API_KEY_HEADER, userKey);
107117
if (accessToken) {
108118
headers.set(constants.AUTH_HEADER, "Bearer " + accessToken);

src/internal/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Code generated. DO NOT EDIT.
22

33
export const API_BASE_URL = "https://api.userhub.com";
4+
export const API_VERSION = "2022-11-15";
45
export const USER_AGENT = "UserHub-JavaScript/0.7.0";
56
export const VERSION = "0.7.0";
67

78
export const AUTH_HEADER = "Authorization";
89
export const API_KEY_HEADER = "UserHub-Api-Key";
10+
export const API_VERSION_HEADER = "UserHub-Api-Version";
911

1012
export const WEBHOOK_ACTION_HEADER = "UserHub-Action";
1113
export const WEBHOOK_AGENT_HEADER = "Webhook-Agent";

test/node/basics.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ test("API GET", async () => {
7575
expect(requests[0].method).equal("GET");
7676
expect(requests[0].url).equal("/admin/v1/users/usr_1");
7777
expect(requests[0].headers["accept"]).equal("application/json");
78+
expect(requests[0].headers["userhub-api-version"]).equal(
79+
constants.API_VERSION,
80+
);
7881
expect(requests[0].headers["authorization"]).equal("Bearer sk_test");
7982
expect(requests[0].headers["content-type"]).toBeUndefined();
8083
expect(requests[0].headers["user-agent"]).equal(constants.USER_AGENT);
@@ -101,6 +104,9 @@ test("API POST", async () => {
101104
expect(requests[0].method).equal("POST");
102105
expect(requests[0].url).equal("/admin/v1/users");
103106
expect(requests[0].headers["accept"]).equal("application/json");
107+
expect(requests[0].headers["userhub-api-version"]).equal(
108+
constants.API_VERSION,
109+
);
104110
expect(requests[0].headers["authorization"]).equal("Bearer sk_test");
105111
expect(requests[0].headers["content-type"]).equal("application/json");
106112
expect(requests[0].headers["user-agent"]).equal(constants.USER_AGENT);
@@ -128,6 +134,9 @@ test("API PATCH", async () => {
128134
expect(requests[0].method).equal("PATCH");
129135
expect(requests[0].url).equal("/admin/v1/users/usr_1?allowMissing=true");
130136
expect(requests[0].headers["accept"]).equal("application/json");
137+
expect(requests[0].headers["userhub-api-version"]).equal(
138+
constants.API_VERSION,
139+
);
131140
expect(requests[0].headers["authorization"]).equal("Bearer sk_test");
132141
expect(requests[0].headers["content-type"]).equal("application/json");
133142
expect(requests[0].headers["user-agent"]).equal(constants.USER_AGENT);
@@ -158,6 +167,9 @@ test("API DELETE", async () => {
158167
expect(requests[0].method).equal("DELETE");
159168
expect(requests[0].url).equal("/admin/v1/users/usr_1");
160169
expect(requests[0].headers["accept"]).equal("application/json");
170+
expect(requests[0].headers["userhub-api-version"]).equal(
171+
constants.API_VERSION,
172+
);
161173
expect(requests[0].headers["authorization"]).equal("Bearer sk_test");
162174
expect(requests[0].headers["content-type"]).toBeUndefined();
163175
expect(requests[0].headers["user-agent"]).equal(constants.USER_AGENT);

0 commit comments

Comments
 (0)