Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit d9578b4

Browse files
committed
use graphql-client utils
1 parent 90515f5 commit d9578b4

File tree

8 files changed

+53
-137
lines changed

8 files changed

+53
-137
lines changed

packages/admin-api-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"!node_modules"
6767
],
6868
"dependencies": {
69-
"@shopify/graphql-client": "^0.3.0"
69+
"@shopify/graphql-client": "^0.5.0"
7070
},
7171
"devDependencies": {
7272
"@babel/core": "^7.21.3",

packages/admin-api-client/src/admin-api-client.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ import {
22
createGraphQLClient,
33
CustomFetchAPI,
44
RequestParams as GQLClientRequestParams,
5+
getCurrentSupportedAPIVersions,
6+
validateDomainAndGetStoreUrl,
7+
validateApiVersion,
58
} from "@shopify/graphql-client";
69

710
import {
811
AdminAPIClient,
912
AdminAPIClientConfig,
1013
AdminAPIClientRequestOptions,
1114
} from "./types";
12-
import { DEFAULT_CONTENT_TYPE, ACCESS_TOKEN_HEADER } from "./constants";
15+
import { DEFAULT_CONTENT_TYPE, ACCESS_TOKEN_HEADER, CLIENT } from "./constants";
1316
import {
14-
getCurrentSupportedAPIVersions,
1517
validateRequiredStoreDomain,
1618
validateRequiredAccessToken,
17-
validateApiVersion,
1819
validateServerSideUsage,
19-
} from "./utilities";
20+
} from "./validations";
2021

2122
const httpRegEx = new RegExp("^(https?:)?//");
2223

@@ -33,11 +34,15 @@ export function createAdminAPIClient({
3334
clientName?: string;
3435
customFetchAPI?: CustomFetchAPI;
3536
}): AdminAPIClient {
36-
const currentSupportedAPIVersions = getCurrentSupportedAPIVersions();
37+
const currentSupportedApiVersions = getCurrentSupportedAPIVersions();
3738

3839
validateServerSideUsage();
3940
validateRequiredStoreDomain(storeDomain);
40-
validateApiVersion(currentSupportedAPIVersions, apiVersion);
41+
validateApiVersion({
42+
client: CLIENT,
43+
currentSupportedApiVersions,
44+
apiVersion,
45+
});
4146
validateRequiredAccessToken(accessToken);
4247

4348
const trimmedStoreDomain = storeDomain.trim();
@@ -47,7 +52,11 @@ export function createAdminAPIClient({
4752

4853
const generateApiUrl = (version?: string) => {
4954
if (version) {
50-
validateApiVersion(currentSupportedAPIVersions, version);
55+
validateApiVersion({
56+
client: CLIENT,
57+
currentSupportedApiVersions,
58+
apiVersion: version,
59+
});
5160
}
5261

5362
const urlApiVersion = (version ?? apiVersion).trim();

packages/admin-api-client/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export const DEFAULT_CONTENT_TYPE = "application/json";
33
export const DEFAULT_CLIENT_VERSION = "ROLLUP_REPLACE_CLIENT_VERSION";
44
export const ACCESS_TOKEN_HEADER = "X-Shopify-Access-Token";
55
export const ERROR_PREFIX = "Admin API Client:";
6+
export const CLIENT = "Admin API Client";

packages/admin-api-client/src/tests/admin-api-client.test.ts

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ describe("Admin API Client", () => {
100100
expect(() =>
101101
createAdminAPIClient({
102102
...config,
103-
// @ts-ignore
104-
storeDomain: undefined,
103+
storeDomain: undefined as any,
105104
})
106105
).toThrow(
107106
new Error("Admin API Client: a valid store domain must be provided")
@@ -112,8 +111,7 @@ describe("Admin API Client", () => {
112111
expect(() =>
113112
createAdminAPIClient({
114113
...config,
115-
// @ts-ignore
116-
storeDomain: " ",
114+
storeDomain: " " as any,
117115
})
118116
).toThrow(
119117
new Error("Admin API Client: a valid store domain must be provided")
@@ -124,8 +122,7 @@ describe("Admin API Client", () => {
124122
expect(() =>
125123
createAdminAPIClient({
126124
...config,
127-
// @ts-ignore
128-
storeDomain: 123,
125+
storeDomain: 123 as any,
129126
})
130127
).toThrow(
131128
new Error("Admin API Client: a valid store domain must be provided")
@@ -136,28 +133,11 @@ describe("Admin API Client", () => {
136133
expect(() =>
137134
createAdminAPIClient({
138135
...config,
139-
// @ts-ignore
140-
apiVersion: undefined,
136+
apiVersion: undefined as any,
141137
})
142138
).toThrow(
143139
new Error(
144-
`Admin API Client: the provided \`apiVersion\` is invalid. Current supported API versions: ${mockApiVersions.join(
145-
", "
146-
)}`
147-
)
148-
);
149-
});
150-
151-
it("throws an error when the api version is not a string", () => {
152-
expect(() =>
153-
createAdminAPIClient({
154-
...config,
155-
// @ts-ignore
156-
apiVersion: { year: 2022, month: 1 },
157-
})
158-
).toThrow(
159-
new Error(
160-
`Admin API Client: the provided \`apiVersion\` is invalid. Current supported API versions: ${mockApiVersions.join(
140+
`Admin API Client: the provided apiVersion ("undefined") is invalid. Current supported API versions: ${mockApiVersions.join(
161141
", "
162142
)}`
163143
)
@@ -175,7 +155,7 @@ describe("Admin API Client", () => {
175155
});
176156

177157
expect(consoleWarnSpy).toHaveBeenCalledWith(
178-
`Admin API Client: the provided \`apiVersion\` (\`2022-07\`) is deprecated or not supported. Current supported API versions: ${mockApiVersions.join(
158+
`Admin API Client: the provided apiVersion ("2022-07") is deprecated or not supported. Current supported API versions: ${mockApiVersions.join(
179159
", "
180160
)}`
181161
);
@@ -185,20 +165,17 @@ describe("Admin API Client", () => {
185165
expect(() =>
186166
createAdminAPIClient({
187167
...config,
188-
// @ts-ignore
189-
accessToken: undefined,
168+
accessToken: undefined as any,
190169
})
191170
).toThrow(
192171
new Error(`Admin API Client: an access token must be provided`)
193172
);
194173
});
195174

196175
it("throws an error when run in a browser environment (window is defined)", () => {
197-
// @ts-ignore
198-
global.window = {};
176+
global.window = {} as any;
199177

200178
expect(() =>
201-
// @ts-ignore
202179
createAdminAPIClient({
203180
...config,
204181
accessToken: "access-token",
@@ -341,7 +318,7 @@ describe("Admin API Client", () => {
341318
client.getApiUrl(version)
342319
).toThrow(
343320
new Error(
344-
`Admin API Client: the provided \`apiVersion\` is invalid. Current supported API versions: ${mockApiVersions.join(
321+
`Admin API Client: the provided apiVersion ("123") is invalid. Current supported API versions: ${mockApiVersions.join(
345322
", "
346323
)}`
347324
)
@@ -357,7 +334,7 @@ describe("Admin API Client", () => {
357334
client.getApiUrl(version);
358335

359336
expect(consoleWarnSpy).toHaveBeenCalledWith(
360-
`Admin API Client: the provided \`apiVersion\` (\`2021-01\`) is deprecated or not supported. Current supported API versions: ${mockApiVersions.join(
337+
`Admin API Client: the provided apiVersion ("2021-01") is deprecated or not supported. Current supported API versions: ${mockApiVersions.join(
361338
", "
362339
)}`
363340
);

packages/admin-api-client/src/utilities/api-versions.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

packages/admin-api-client/src/utilities/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/admin-api-client/src/utilities/validations.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ERROR_PREFIX } from "./constants";
2+
3+
export function validateRequiredStoreDomain(storeDomain: string | undefined) {
4+
if (
5+
!storeDomain ||
6+
typeof storeDomain !== "string" ||
7+
storeDomain.trim().length < 1
8+
) {
9+
throw new Error(`${ERROR_PREFIX} a valid store domain must be provided`);
10+
}
11+
}
12+
13+
export function validateRequiredAccessToken(accessToken: string) {
14+
if (!accessToken) {
15+
throw new Error(`${ERROR_PREFIX} an access token must be provided`);
16+
}
17+
}
18+
19+
export function validateServerSideUsage() {
20+
if (typeof window !== "undefined") {
21+
throw new Error(
22+
`${ERROR_PREFIX} this client should not be used in the browser`
23+
);
24+
}
25+
}

0 commit comments

Comments
 (0)