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

Commit 7558b3e

Browse files
committed
Revamp REST client to match new clients
1 parent 6a78c48 commit 7558b3e

File tree

11 files changed

+757
-88
lines changed

11 files changed

+757
-88
lines changed

packages/shopify-api/lib/clients/admin/__tests__/admin_rest_client.test.ts

Lines changed: 502 additions & 0 deletions
Large diffs are not rendered by default.

packages/shopify-api/lib/clients/admin/__tests__/resources/load-rest-resources.test.ts

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,59 @@ import {shopifyApi} from '../../../..';
55
import {restResources} from './test-resources';
66

77
describe('Load REST resources', () => {
8-
it('sets up objects with a client', async () => {
9-
const shopify = shopifyApi(testConfig({restResources}));
8+
describe('using new clients', () => {
9+
it('sets up objects with a client', async () => {
10+
const shopify = shopifyApi(testConfig({restResources}));
1011

11-
expect(shopify.rest).toHaveProperty('FakeResource');
12-
expect(shopify.rest.FakeResource.Client).toBeDefined();
12+
expect(shopify.rest).toHaveProperty('FakeResource');
13+
expect(shopify.rest.FakeResource.client).toBeDefined();
14+
});
15+
16+
it('warns if the API versions mismatch', async () => {
17+
const shopify = shopifyApi(
18+
testConfig({restResources, apiVersion: '2020-01' as any as ApiVersion}),
19+
);
20+
21+
expect(shopify.config.logger.log).toHaveBeenCalledWith(
22+
LogSeverity.Warning,
23+
expect.stringContaining(
24+
`Loading REST resources for API version ${LATEST_API_VERSION}, which doesn't match the default 2020-01`,
25+
),
26+
);
27+
28+
expect(shopify.rest).toHaveProperty('FakeResource');
29+
expect(shopify.rest.FakeResource.client).toBeDefined();
30+
});
1331
});
1432

15-
it('warns if the API versions mismatch', async () => {
16-
const shopify = shopifyApi(
17-
testConfig({restResources, apiVersion: '2020-01' as any as ApiVersion}),
18-
);
33+
describe('using clients', () => {
34+
it('sets up objects with a client', async () => {
35+
const shopify = shopifyApi(
36+
testConfig({restResources, future: {unstable_newApiClients: false}}),
37+
);
38+
39+
expect(shopify.rest).toHaveProperty('FakeResource');
40+
expect(shopify.rest.FakeResource.Client).toBeDefined();
41+
});
42+
43+
it('warns if the API versions mismatch', async () => {
44+
const shopify = shopifyApi(
45+
testConfig({
46+
restResources,
47+
apiVersion: '2020-01' as any as ApiVersion,
48+
future: {unstable_newApiClients: false},
49+
}),
50+
);
1951

20-
expect(shopify.config.logger.log).toHaveBeenCalledWith(
21-
LogSeverity.Warning,
22-
expect.stringContaining(
23-
`Loading REST resources for API version ${LATEST_API_VERSION}, which doesn't match the default 2020-01`,
24-
),
25-
);
52+
expect(shopify.config.logger.log).toHaveBeenCalledWith(
53+
LogSeverity.Warning,
54+
expect.stringContaining(
55+
`Loading REST resources for API version ${LATEST_API_VERSION}, which doesn't match the default 2020-01`,
56+
),
57+
);
2658

27-
expect(shopify.rest).toHaveProperty('FakeResource');
28-
expect(shopify.rest.FakeResource.Client).toBeDefined();
59+
expect(shopify.rest).toHaveProperty('FakeResource');
60+
expect(shopify.rest.FakeResource.Client).toBeDefined();
61+
});
2962
});
3063
});

packages/shopify-api/lib/clients/admin/graphql/admin_client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import {createAdminApiClient} from '@shopify/admin-api-client';
22

33
import {logger} from '../../../logger';
44
import {ConfigInterface} from '../../../base-types';
5-
import {AdminClientFactory} from '../types';
5+
import {AdminGraphqlClientFactory} from '../types';
66
import {abstractFetch} from '../../../../runtime';
77
import {clientLoggerFactory, getUserAgent} from '../../common';
88
import {GraphqlQueryError} from '../../../error';
99

1010
export function adminGraphqlClientFactory(
1111
config: ConfigInterface,
12-
): AdminClientFactory {
12+
): AdminGraphqlClientFactory {
1313
return ({session, apiVersion, retries}) => {
1414
if (apiVersion && apiVersion !== config.apiVersion) {
1515
logger(config).debug(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export {adminGraphqlClientFactory} from './graphql/admin_client';
2+
export {RestClient, adminRestClientFactory} from './rest/rest_client';

packages/shopify-api/lib/clients/admin/rest/rest_client.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {getHeader} from '../../../../runtime/http';
22
import {ApiVersion, ShopifyHeader} from '../../../types';
33
import {ConfigInterface} from '../../../base-types';
4-
import {RequestParams} from '../../http_client/types';
4+
import {DataType, RequestParams} from '../../http_client/types';
55
import * as ShopifyErrors from '../../../error';
66
import {HttpClient} from '../../http_client/http_client';
77
import {Session} from '../../../session/session';
@@ -11,6 +11,7 @@ import {
1111
PageInfo,
1212
RestClientParams,
1313
PageInfoParams,
14+
RestClient as RestClientFunction,
1415
} from '../types';
1516

1617
export interface RestClientClassParams {
@@ -155,3 +156,50 @@ export function restClientClass(
155156

156157
return NewRestClient as typeof RestClient;
157158
}
159+
160+
export function adminRestClientFactory(config: ConfigInterface) {
161+
const RestClientClass = restClientClass({config});
162+
163+
return (params: RestClientParams): RestClientFunction => {
164+
const client = new RestClientClass(params);
165+
166+
return {
167+
get: (path, options) =>
168+
client.get({
169+
path,
170+
type: DataType.JSON,
171+
data: options?.data,
172+
extraHeaders: options?.headers,
173+
query: options?.query,
174+
tries: options?.tries,
175+
}),
176+
post: (path, options) =>
177+
client.post({
178+
path,
179+
type: DataType.JSON,
180+
data: options?.data,
181+
extraHeaders: options?.headers,
182+
query: options?.query,
183+
tries: options?.tries,
184+
}),
185+
put: (path, options) =>
186+
client.put({
187+
path,
188+
type: DataType.JSON,
189+
data: options?.data,
190+
extraHeaders: options?.headers,
191+
query: options?.query,
192+
tries: options?.tries,
193+
}),
194+
delete: (path, options) =>
195+
client.delete({
196+
path,
197+
type: DataType.JSON,
198+
data: options?.data,
199+
extraHeaders: options?.headers,
200+
query: options?.query,
201+
tries: options?.tries,
202+
}),
203+
};
204+
};
205+
}

packages/shopify-api/lib/clients/admin/types.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {createAdminApiClient} from '@shopify/admin-api-client';
22

3-
import type {ClientArgs} from '../types';
3+
import type {ClientArgs, HeaderParams} from '../types';
44
import {ApiVersion} from '../../types';
55
import {Session} from '../../session/session';
66
import {RequestReturn, QueryParams} from '../http_client/types';
@@ -28,6 +28,37 @@ export interface RestClientParams {
2828
apiVersion?: ApiVersion;
2929
}
3030

31-
export type AdminClientFactory = (
31+
export type AdminGraphqlClientFactory = (
3232
args: ClientArgs,
3333
) => ReturnType<typeof createAdminApiClient>;
34+
35+
interface RestRequestOptions {
36+
data?: {[key: string]: unknown} | string;
37+
query?: {[key: string]: QueryParams};
38+
headers?: HeaderParams;
39+
tries?: number;
40+
}
41+
interface RestRequestOptionsWithData extends RestRequestOptions {
42+
data: NonNullable<RestRequestOptions['data']>;
43+
}
44+
45+
export interface RestClient {
46+
get: <T = unknown>(
47+
path: string,
48+
params?: RestRequestOptions,
49+
) => Promise<RestRequestReturn<T>>;
50+
post: <T = unknown>(
51+
path: string,
52+
params: RestRequestOptionsWithData,
53+
) => Promise<RestRequestReturn<T>>;
54+
put: <T = unknown>(
55+
path: string,
56+
params: RestRequestOptionsWithData,
57+
) => Promise<RestRequestReturn<T>>;
58+
delete: <T = unknown>(
59+
path: string,
60+
params?: RestRequestOptions,
61+
) => Promise<RestRequestReturn<T>>;
62+
}
63+
64+
export type AdminRestClientFactory = (params: RestClientParams) => RestClient;

packages/shopify-api/lib/clients/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {ConfigInterface} from '../base-types';
22

33
import {httpClientClass} from './http_client/http_client';
4-
import {restClientClass} from './admin/rest/rest_client';
4+
import {
5+
adminRestClientFactory,
6+
restClientClass,
7+
} from './admin/rest/rest_client';
58
import {graphqlClientClass} from './legacy_graphql/legacy_admin_client';
69
import {storefrontClientClass} from './legacy_graphql/legacy_storefront_client';
710
import {graphqlProxy} from './graphql_proxy/graphql_proxy';
@@ -17,6 +20,7 @@ export function clientClasses<Config extends ConfigInterface>(
1720
if (config.future?.unstable_newApiClients) {
1821
return {
1922
admin: {
23+
rest: adminRestClientFactory(config),
2024
graphql: adminGraphqlClientFactory(config),
2125
},
2226
storefront: storefrontGraphqlClientFactory(config),

packages/shopify-api/lib/clients/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {GraphqlClient} from './legacy_graphql/legacy_admin_client';
66
import {StorefrontClient} from './legacy_graphql/legacy_storefront_client';
77
import {GraphqlProxy} from './graphql_proxy/types';
88
import {RestClient as RestClientClass} from './admin/rest/rest_client';
9-
import {AdminClientFactory} from './admin/types';
9+
import {AdminGraphqlClientFactory, AdminRestClientFactory} from './admin/types';
1010
import {StorefrontClientFactory} from './storefront/types';
1111

1212
export * from './http_client/types';
@@ -25,7 +25,8 @@ export type ShopifyClients<
2525
> = FeatureEnabled<Future, 'unstable_newApiClients'> extends true
2626
? {
2727
admin: {
28-
graphql: AdminClientFactory;
28+
rest: AdminRestClientFactory;
29+
graphql: AdminGraphqlClientFactory;
2930
};
3031
storefront: StorefrontClientFactory;
3132
graphqlProxy: GraphqlProxy;

packages/shopify-api/lib/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {shopifyWebhooks, ShopifyWebhooks} from './webhooks';
1313
import {shopifyBilling, ShopifyBilling} from './billing';
1414
import {logger, ShopifyLogger} from './logger';
1515
import {SHOPIFY_API_LIBRARY_VERSION} from './version';
16-
import {restClientClass} from './clients/admin/rest/rest_client';
1716

1817
export * from './error';
1918
export * from './session/classes';
@@ -72,11 +71,11 @@ export function shopifyApi<
7271
};
7372

7473
if (restResources) {
75-
shopify.rest = loadRestResources({
76-
resources: restResources,
77-
config: validatedConfig,
78-
RestClient: restClientClass({config: validatedConfig}),
79-
});
74+
shopify.rest = loadRestResources(
75+
validatedConfig,
76+
shopify.clients,
77+
restResources,
78+
);
8079
}
8180

8281
shopify.logger

0 commit comments

Comments
 (0)