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

Commit 0a739a7

Browse files
committed
Add REST resources
1 parent 139dd9f commit 0a739a7

File tree

601 files changed

+86228
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

601 files changed

+86228
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@
7272
"dist/*",
7373
"!tsconfig.tsbuildinfo"
7474
]
75-
}
75+
}

src/__tests__/base-rest-resource.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Session} from '../auth/session';
2-
import {RestResourceRequestError} from '../error';
2+
import {ApiVersion} from '../base-types';
3+
import {Context} from '../context';
4+
import {RestResourceRequestError, RestResourceError} from '../error';
35

46
import FakeResource from './fake-resource';
57
import FakeResourceWithCustomPrefix from './fake-resource-with-custom-prefix';
@@ -393,4 +395,14 @@ describe('Base REST resource', () => {
393395
headers,
394396
}).toMatchMadeHttpRequest();
395397
});
398+
399+
it('throws an error if the API versions mismatch', async () => {
400+
Context.API_VERSION = ApiVersion.January22;
401+
402+
await expect(FakeResource.all({session})).rejects.toThrowError(
403+
new RestResourceError(
404+
`Current Context.API_VERSION '${ApiVersion.January22}' does not match resource version ${ApiVersion.Unstable}`,
405+
),
406+
);
407+
});
396408
});

src/__tests__/fake-resource-with-custom-prefix.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Base, {ResourcePath} from '../base-rest-resource';
22
import {SessionInterface} from '../auth/session/types';
3+
import {ApiVersion} from '../base-types';
34

45
interface FakeResourceWithCustomPrefixFindArgs {
56
session: SessionInterface;
67
id: string | number;
78
}
89

910
export default class FakeResourceWithCustomPrefix extends Base {
11+
public static API_VERSION = ApiVersion.Unstable;
1012
protected static NAME = 'fake_resource_with_custom_prefix';
1113
protected static PLURAL_NAME = 'fake_resource_with_custom_prefixes';
1214
protected static CUSTOM_PREFIX = '/admin/custom_prefix';

src/__tests__/fake-resource.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Base, {ParamSet, ResourcePath} from '../base-rest-resource';
22
import {SessionInterface} from '../auth/session/types';
3+
import {ApiVersion} from '../base-types';
34

45
interface FakeResourceFindArgs {
56
params?: ParamSet;
@@ -20,6 +21,7 @@ interface FakeResourceCustomArgs {
2021
}
2122

2223
export default class FakeResource extends Base {
24+
public static API_VERSION = ApiVersion.Unstable;
2325
protected static NAME = 'fake_resource';
2426
protected static PLURAL_NAME = 'fake_resources';
2527

src/base-rest-resource.ts

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {SessionInterface} from './auth/session/types';
77
import {RestClient} from './clients/rest';
88
import {RestRequestReturn} from './clients/rest/types';
99
import {DataType, GetRequestParams} from './clients/http_client/types';
10+
import {ApiVersion} from './base-types';
11+
import {Context} from './context';
1012

1113
export interface IdSet {
1214
[id: string]: string | number | null;
@@ -56,6 +58,7 @@ class Base {
5658
// For instance attributes
5759
[key: string]: any;
5860

61+
public static API_VERSION: ApiVersion;
5962
public static NEXT_PAGE_INFO: GetRequestParams | undefined;
6063
public static PREV_PAGE_INFO: GetRequestParams | undefined;
6164

@@ -97,6 +100,12 @@ class Base {
97100
body,
98101
entity,
99102
}: RequestArgs): Promise<RestRequestReturn> {
103+
if (Context.API_VERSION !== this.API_VERSION) {
104+
throw new RestResourceError(
105+
`Current Context.API_VERSION '${Context.API_VERSION}' does not match resource version ${this.API_VERSION}`,
106+
);
107+
}
108+
100109
const client = new RestClient(session.shop, session.accessToken);
101110

102111
const path = this.getPath({http_method, operation, urlIds, entity});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import Base, {ResourcePath} from '../../base-rest-resource';
2+
import {SessionInterface} from '../../auth/session/types';
3+
import {ApiVersion} from '../../base-types';
4+
5+
import {Currency} from './currency';
6+
import {Customer} from './customer';
7+
import {DiscountCode} from './discount_code';
8+
9+
interface CheckoutsArgs {
10+
[key: string]: unknown;
11+
session: SessionInterface;
12+
since_id?: unknown;
13+
created_at_min?: unknown;
14+
created_at_max?: unknown;
15+
updated_at_min?: unknown;
16+
updated_at_max?: unknown;
17+
status?: unknown;
18+
limit?: unknown;
19+
}
20+
21+
export class AbandonedCheckout extends Base {
22+
public static API_VERSION = ApiVersion.April21;
23+
24+
protected static NAME = 'abandoned_checkout';
25+
protected static PLURAL_NAME = 'abandoned_checkouts';
26+
protected static HAS_ONE: {[key: string]: typeof Base} = {
27+
currency: Currency,
28+
customer: Customer
29+
};
30+
protected static HAS_MANY: {[key: string]: typeof Base} = {
31+
discount_codes: DiscountCode
32+
};
33+
protected static PATHS: ResourcePath[] = [
34+
{http_method: "get", operation: "checkouts", ids: [], path: "checkouts.json"},
35+
{http_method: "get", operation: "checkouts", ids: [], path: "checkouts.json"}
36+
];
37+
38+
public static async checkouts(
39+
{
40+
session,
41+
since_id = null,
42+
created_at_min = null,
43+
created_at_max = null,
44+
updated_at_min = null,
45+
updated_at_max = null,
46+
status = null,
47+
limit = null,
48+
...otherArgs
49+
}: CheckoutsArgs
50+
): Promise<unknown> {
51+
const response = await AbandonedCheckout.request({
52+
http_method: "get",
53+
operation: "checkouts",
54+
session: session,
55+
urlIds: {},
56+
params: {since_id: since_id, created_at_min: created_at_min, created_at_max: created_at_max, updated_at_min: updated_at_min, updated_at_max: updated_at_max, status: status, limit: limit, ...otherArgs},
57+
body: {},
58+
entity: null,
59+
});
60+
61+
return response ? response.body : null;
62+
}
63+
64+
public abandoned_checkout_url: string | null;
65+
public billing_address: {[key: string]: unknown} | null;
66+
public buyer_accepts_marketing: boolean | null;
67+
public buyer_accepts_sms_marketing: boolean | null;
68+
public cart_token: string | null;
69+
public closed_at: string | null;
70+
public completed_at: string | null;
71+
public created_at: string | null;
72+
public currency: Currency | null | {[key: string]: any};
73+
public customer: Customer | null | {[key: string]: any};
74+
public customer_locale: string | null;
75+
public device_id: number | null;
76+
public discount_codes: DiscountCode[] | null | {[key: string]: any};
77+
public email: string | null;
78+
public gateway: string | null;
79+
public id: number | null;
80+
public landing_site: string | null;
81+
public line_items: {[key: string]: unknown} | null;
82+
public location_id: number | null;
83+
public note: string | null;
84+
public phone: string | null;
85+
public presentment_currency: string | null;
86+
public referring_site: string | null;
87+
public shipping_address: {[key: string]: unknown} | null;
88+
public shipping_lines: {[key: string]: unknown} | null;
89+
public sms_marketing_phone: string | null;
90+
public source_name: string | null;
91+
public subtotal_price: string | null;
92+
public tax_lines: {[key: string]: unknown} | null;
93+
public taxes_included: boolean | null;
94+
public token: string | null;
95+
public total_discounts: string | null;
96+
public total_duties: string | null;
97+
public total_line_items_price: string | null;
98+
public total_price: string | null;
99+
public total_tax: string | null;
100+
public total_weight: number | null;
101+
public updated_at: string | null;
102+
public user_id: number | null;
103+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Base, {ResourcePath} from '../../base-rest-resource';
2+
import {SessionInterface} from '../../auth/session/types';
3+
import {ApiVersion} from '../../base-types';
4+
5+
interface AllArgs {
6+
[key: string]: unknown;
7+
session: SessionInterface;
8+
}
9+
10+
export class AccessScope extends Base {
11+
public static API_VERSION = ApiVersion.April21;
12+
13+
protected static NAME = 'access_scope';
14+
protected static PLURAL_NAME = 'access_scopes';
15+
protected static HAS_ONE: {[key: string]: typeof Base} = {};
16+
protected static HAS_MANY: {[key: string]: typeof Base} = {};
17+
protected static CUSTOM_PREFIX: string | null = "/admin/oauth";
18+
protected static PATHS: ResourcePath[] = [
19+
{http_method: "get", operation: "get", ids: [], path: "access_scopes.json"}
20+
];
21+
22+
public static async all(
23+
{
24+
session,
25+
...otherArgs
26+
}: AllArgs
27+
): Promise<AccessScope[]> {
28+
const response = await AccessScope.baseFind({
29+
session: session,
30+
urlIds: {},
31+
params: {...otherArgs},
32+
});
33+
34+
return response as AccessScope[];
35+
}
36+
37+
public handle: string | null;
38+
public access_scopes: {[key: string]: unknown}[] | null;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Base, {ResourcePath} from '../../base-rest-resource';
2+
import {SessionInterface} from '../../auth/session/types';
3+
import {ApiVersion} from '../../base-types';
4+
5+
interface FindArgs {
6+
session: SessionInterface;
7+
id: number | string;
8+
}
9+
interface DeleteArgs {
10+
session: SessionInterface;
11+
id: number | string;
12+
}
13+
14+
export class AndroidPayKey extends Base {
15+
public static API_VERSION = ApiVersion.April21;
16+
17+
protected static NAME = 'android_pay_key';
18+
protected static PLURAL_NAME = 'android_pay_keys';
19+
protected static HAS_ONE: {[key: string]: typeof Base} = {};
20+
protected static HAS_MANY: {[key: string]: typeof Base} = {};
21+
protected static PATHS: ResourcePath[] = [
22+
{http_method: "post", operation: "post", ids: [], path: "android_pay_keys.json"},
23+
{http_method: "get", operation: "get", ids: ["id"], path: "android_pay_keys/<id>.json"},
24+
{http_method: "delete", operation: "delete", ids: ["id"], path: "android_pay_keys/<id>.json"}
25+
];
26+
27+
public static async find(
28+
{
29+
session,
30+
id
31+
}: FindArgs
32+
): Promise<AndroidPayKey | null> {
33+
const result = await AndroidPayKey.baseFind({
34+
session: session,
35+
urlIds: {id: id},
36+
params: {},
37+
});
38+
return result ? result[0] as AndroidPayKey : null;
39+
}
40+
41+
public static async delete(
42+
{
43+
session,
44+
id
45+
}: DeleteArgs
46+
): Promise<unknown> {
47+
const response = await AndroidPayKey.request({
48+
http_method: "delete",
49+
operation: "delete",
50+
session: session,
51+
urlIds: {id: id},
52+
params: {},
53+
});
54+
55+
return response ? response.body : null;
56+
}
57+
58+
public id: number | null;
59+
public public_key: string | null;
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import Base, {ResourcePath} from '../../base-rest-resource';
2+
import {SessionInterface} from '../../auth/session/types';
3+
import {ApiVersion} from '../../base-types';
4+
5+
interface FindArgs {
6+
session: SessionInterface;
7+
id: number | string;
8+
}
9+
interface DeleteArgs {
10+
session: SessionInterface;
11+
id: number | string;
12+
}
13+
interface CsrArgs {
14+
[key: string]: unknown;
15+
session: SessionInterface;
16+
id: number | string;
17+
}
18+
19+
export class ApplePayCertificate extends Base {
20+
public static API_VERSION = ApiVersion.April21;
21+
22+
protected static NAME = 'apple_pay_certificate';
23+
protected static PLURAL_NAME = 'apple_pay_certificates';
24+
protected static HAS_ONE: {[key: string]: typeof Base} = {};
25+
protected static HAS_MANY: {[key: string]: typeof Base} = {};
26+
protected static PATHS: ResourcePath[] = [
27+
{http_method: "post", operation: "post", ids: [], path: "apple_pay_certificates.json"},
28+
{http_method: "get", operation: "get", ids: ["id"], path: "apple_pay_certificates/<id>.json"},
29+
{http_method: "put", operation: "put", ids: ["id"], path: "apple_pay_certificates/<id>.json"},
30+
{http_method: "delete", operation: "delete", ids: ["id"], path: "apple_pay_certificates/<id>.json"},
31+
{http_method: "get", operation: "csr", ids: ["id"], path: "apple_pay_certificates/<id>/csr.json"}
32+
];
33+
34+
public static async find(
35+
{
36+
session,
37+
id
38+
}: FindArgs
39+
): Promise<ApplePayCertificate | null> {
40+
const result = await ApplePayCertificate.baseFind({
41+
session: session,
42+
urlIds: {id: id},
43+
params: {},
44+
});
45+
return result ? result[0] as ApplePayCertificate : null;
46+
}
47+
48+
public static async delete(
49+
{
50+
session,
51+
id
52+
}: DeleteArgs
53+
): Promise<unknown> {
54+
const response = await ApplePayCertificate.request({
55+
http_method: "delete",
56+
operation: "delete",
57+
session: session,
58+
urlIds: {id: id},
59+
params: {},
60+
});
61+
62+
return response ? response.body : null;
63+
}
64+
65+
public static async csr(
66+
{
67+
session,
68+
id,
69+
...otherArgs
70+
}: CsrArgs
71+
): Promise<unknown> {
72+
const response = await ApplePayCertificate.request({
73+
http_method: "get",
74+
operation: "csr",
75+
session: session,
76+
urlIds: {id: id},
77+
params: {...otherArgs},
78+
body: {},
79+
entity: null,
80+
});
81+
82+
return response ? response.body : null;
83+
}
84+
85+
public id: number | null;
86+
public merchant_id: string | null;
87+
public status: string | null;
88+
}

0 commit comments

Comments
 (0)