UNPKG

@shopify/cli-kit

Version:

A set of utilities, interfaces, and models that are common across all the platform features

69 lines 2.9 kB
import { graphqlRequestDoc } from './graphql.js'; import { addCursorAndFiltersToAppLogsUrl } from './utilities.js'; import { appManagementFqdn } from '../context/fqdn.js'; import { setNextDeprecationDate } from '../../../private/node/context/deprecations-store.js'; import { buildHeaders } from '../../../private/node/api/headers.js'; import Bottleneck from 'bottleneck'; // API Rate limiter for partners API (Limit is 10 requests per second) // Jobs are launched every 150ms to add an extra 50ms margin per request. // Only 10 requests can be executed concurrently. const limiter = new Bottleneck({ minTime: 150, maxConcurrent: 10, }); async function setupRequest(token) { const api = 'App Management'; const fqdn = await appManagementFqdn(); const url = `https://${fqdn}/app_management/unstable/graphql.json`; return { token, api, url, responseOptions: { onResponse: handleDeprecations }, }; } export const appManagementHeaders = (token) => { return buildHeaders(token); }; export const appManagementAppLogsUrl = async (organizationId, cursor, filters) => { const fqdn = await appManagementFqdn(); const url = `https://${fqdn}/app_management/unstable/organizations/${organizationId}/app_logs/poll`; return addCursorAndFiltersToAppLogsUrl(url, cursor, filters); }; /** * Executes an org-scoped GraphQL query against the App Management API. Uses typed documents. * * @param options - The options for the request. * @returns The response of the query of generic type <T>. */ export async function appManagementRequestDoc(options) { const cacheExtraKey = options.cacheOptions?.cacheExtraKey ?? ''; const newCacheOptions = options.cacheOptions ? { ...options.cacheOptions, cacheExtraKey } : undefined; const result = limiter.schedule(async () => graphqlRequestDoc({ ...(await setupRequest(options.token)), query: options.query, variables: options.variables, cacheOptions: newCacheOptions, preferredBehaviour: options.requestOptions?.requestMode, unauthorizedHandler: options.unauthorizedHandler, })); return result; } /** * Sets the next deprecation date from [GraphQL response extensions](https://www.apollographql.com/docs/resources/graphql-glossary/#extensions) * if `response.extensions.deprecations` objects contain a `supportedUntilDate` (ISO 8601-formatted string). * * @param response - The response of the query. */ export function handleDeprecations(response) { if (!response.extensions) return; const deprecationDates = []; for (const deprecation of response.extensions.deprecations) { if (deprecation.supportedUntilDate) { deprecationDates.push(new Date(deprecation.supportedUntilDate)); } } setNextDeprecationDate(deprecationDates); } //# sourceMappingURL=app-management.js.map