Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 32x 15x 15x 15x 13x 15x 15x 3x 3x 2x 1x 1x 1x 2x | import CoreFormsEnum, { getFormName } from '@orchesty/nodejs-sdk/dist/lib/Application/Base/CoreFormsEnum';
import { ApplicationInstall } from '@orchesty/nodejs-sdk/dist/lib/Application/Database/ApplicationInstall';
import Field from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Form/Field';
import FieldType from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Form/FieldType';
import Form from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Form/Form';
import FormStack from '@orchesty/nodejs-sdk/dist/lib/Application/Model/Form/FormStack';
import { ABasicApplication, TOKEN } from '@orchesty/nodejs-sdk/dist/lib/Authorization/Type/Basic/ABasicApplication';
import CurlSender from '@orchesty/nodejs-sdk/dist/lib/Transport/Curl/CurlSender';
import RequestDto from '@orchesty/nodejs-sdk/dist/lib/Transport/Curl/RequestDto';
import { HttpMethods, parseHttpMethod } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import AProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/AProcessDto';
import { CommonHeaders, JSON_TYPE } from '@orchesty/nodejs-sdk/dist/lib/Utils/Headers';
import ProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/ProcessDto';
export const NAME = 'shopify';
export const API_VERSION = '2024-01';
export const SHOPIFY_URL = 'shopifyUrl';
export const PREMIUM_PLAN = 'premium';
export const CURRENCY = 'currency';
const API_KEY_HEADER = 'X-Shopify-Access-Token';
const SHOP_INFO_URL = `admin/api/${API_VERSION}/shop.json`;
export default abstract class ABaseShopify extends ABasicApplication {
public constructor(protected readonly curlSender: CurlSender) {
super();
}
public getDescription(): string {
return 'Simple way to create an online store to list products, collect credit card payments, and ship your goods';
}
public getName(): string {
return NAME;
}
public getPublicName(): string {
return 'Shopify';
}
public getLogo(): string | null {
return 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDI1LjAuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IlZyc3R2YV8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIKCSB2aWV3Qm94PSIwIDAgMTAwIDEwMCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgMTAwIDEwMDsiIHhtbDpzcGFjZT0icHJlc2VydmUiPgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgoJLnN0MHtmaWxsOiM5NUJGNDY7fQoJLnN0MXtmaWxsOiM1RThFM0U7fQoJLnN0MntmaWxsOiNGRkZGRkY7fQo8L3N0eWxlPgo8Zz4KCTxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik04Mi45LDE5LjZjLTAuMS0wLjUtMC41LTAuOC0wLjktMC44Yy0wLjQsMC04LTAuNi04LTAuNnMtNS4zLTUuMy01LjktNS45Yy0wLjYtMC42LT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wLDUuMiwxMy40LDcuMSwxMy40LDE5LjIKCQljMCw5LjUtNiwxNS42LTE0LjEsMTUuNmMtOS43LDAtMTQuNy02LjEtMTQuNy02LjFsMi42LTguNmMwLDAsNS4xLDQuNCw5LjQsNC40YzIuOCwwLDQtMi4yLDQtMy44YzAtNi43LTExLTctMTEtMTguMQoJCWMwLTkuMyw2LjctMTguMywyMC4xLTE4LjNDNTAsMzQuMyw1Mi42LDM1LjgsNTIuNiwzNS44Ii8+CjwvZz4KPC9zdmc+Cg==';
}
public getRequestDto(
dto: AProcessDto,
applicationInstall: ApplicationInstall,
method: HttpMethods | string,
url?: string,
data?: string,
): RequestDto {
const headers = {
[API_KEY_HEADER]: this.getTokenForRequest(applicationInstall),
[CommonHeaders.ACCEPT]: JSON_TYPE,
[CommonHeaders.CONTENT_TYPE]: JSON_TYPE,
};
let urlx = url ?? '';
if (!urlx.startsWith('http')) {
urlx = `${this.getDecoratedUrl(applicationInstall)}/${urlx}`;
}
return new RequestDto(urlx, parseHttpMethod(method), dto, data, headers);
}
public getDecoratedUrl(app: ApplicationInstall): string {
return app
.getSettings()?.[CoreFormsEnum.AUTHORIZATION_FORM]?.[SHOPIFY_URL] ?? '';
}
public isAuthorized(applicationInstall: ApplicationInstall): boolean {
const authorizationForm = applicationInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM];
return super.isAuthorized(applicationInstall)
&& authorizationForm?.[TOKEN]
&& authorizationForm?.[SHOPIFY_URL]
&& authorizationForm?.[PREMIUM_PLAN] !== undefined;
}
public getFormStack(): FormStack {
const form = new Form(CoreFormsEnum.AUTHORIZATION_FORM, getFormName(CoreFormsEnum.AUTHORIZATION_FORM));
form
.addField(new Field(FieldType.TEXT, TOKEN, 'Admin API access token', undefined, true))
.addField(
new Field(FieldType.URL, SHOPIFY_URL, 'Shopify URL', undefined, true)
.setDescription('Format: https://eshop.myshopify.com'),
);
return new FormStack().addForm(form);
}
public getNextPageFromHeaders(headers: Record<string, unknown>): string | undefined {
const linkHeaders = (headers.Link ?? headers.link) as string;
if (linkHeaders) {
const nextLinkHeader = linkHeaders.split(',').find((link) => link.includes('rel=next') || link.includes('rel="next"'));
// eslint-disable-next-line prefer-named-capture-group
const nextLink = (/<(.+)>/).exec(nextLinkHeader ?? '');
Eif (nextLink) {
return nextLink[1];
}
return undefined;
}
return undefined;
}
protected getTokenForRequest(appInstall: ApplicationInstall): string {
return appInstall.getSettings()[CoreFormsEnum.AUTHORIZATION_FORM][TOKEN];
}
protected async checkShopPlan(applicationInstall: ApplicationInstall): Promise<void> {
const requestDto = this.getRequestDto(
ProcessDto.createForFormRequest(NAME, applicationInstall.getUser(), crypto.randomUUID()),
applicationInstall,
HttpMethods.GET,
SHOP_INFO_URL,
);
const res = await this.curlSender
// eslint-disable-next-line @typescript-eslint/naming-convention
.send<{ shop: { plan_name: string; currency: string } }>(requestDto, [200, 404]);
const { shop } = res.getJsonBody();
let premium = false;
if (shop.plan_name !== 'plus') {
premium = true;
}
applicationInstall.addSettings(
{ [CoreFormsEnum.AUTHORIZATION_FORM]: {
[PREMIUM_PLAN]: premium,
[CURRENCY]: shop.currency,
},
},
);
}
}
|