diff --git a/packages/cli/src/lib/admin-api-client.ts b/packages/cli/src/lib/admin-api-client.ts new file mode 100644 index 0000000000..efb28c9623 --- /dev/null +++ b/packages/cli/src/lib/admin-api-client.ts @@ -0,0 +1,63 @@ +type AdminAPIResponse = { + errors: Error[] | null; + data: object | null; +}; + +const defaultOptions = {variables: {}}; + +type DefaultOptions = { + variables: Record | undefined; +}; + +/** + * A basic Admin API fetch-based client. + */ +export function createAdminClient({ + adminApiVersion, + privateAdminToken, + storeDomain, +}: { + adminApiVersion: string; + privateAdminToken: string; + storeDomain: string; +}) { + async function admin( + query: string | null, + options?: DefaultOptions, + ): Promise { + if (!query) { + throw new Error('Must provide a `query` to the admin client'); + } + + const endpoint = `${storeDomain}/admin/api/${adminApiVersion}/graphql.json`; + const reqInit = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-Shopify-Access-Token': privateAdminToken, + }, + body: JSON.stringify({ + query, + variables: options?.variables || defaultOptions.variables, + }), + }; + + const request = await fetch(endpoint, reqInit); + + if (!request.ok) { + throw new Error( + `graphql api request not ok ${request.status} ${request.statusText}`, + ); + } + + const response: AdminAPIResponse = await request.json(); + + if (response?.errors?.length) { + throw new Error(response.errors[0].message); + } + + return response.data as T; + } + + return {admin}; +}