@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
111 lines • 4.27 kB
JavaScript
import { graphqlRequest, graphqlRequestDoc, } from './graphql.js';
import { addCursorAndFiltersToAppLogsUrl } from './utilities.js';
import { partnersFqdn } from '../context/fqdn.js';
import { setNextDeprecationDate } from '../../../private/node/context/deprecations-store.js';
import { getPackageManager } from '../node-package-manager.js';
import { cwd } from '../path.js';
import { AbortError } from '../error.js';
import { formatPackageManagerCommand } from '../output.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,
});
/**
* Sets up the request to the Partners API.
*
* @param token - Partners token.
*/
async function setupRequest(token) {
const api = 'Partners';
const fqdn = await partnersFqdn();
const url = `https://${fqdn}/api/cli/graphql`;
return {
token,
api,
url,
responseOptions: { onResponse: handleDeprecations },
};
}
/**
* Executes a GraphQL query against the Partners API.
*
* @param query - GraphQL query to execute.
* @param token - Partners token.
* @param variables - GraphQL variables to pass to the query.
* @param cacheOptions - Cache options.
* @param preferredBehaviour - Preferred behaviour for the request.
* @param unauthorizedHandler - Optional handler for unauthorized requests.
* @returns The response of the query of generic type <T>.
*/
export async function partnersRequest(query, token, variables, cacheOptions, preferredBehaviour, unauthorizedHandler) {
const opts = await setupRequest(token);
const result = limiter.schedule(() => graphqlRequest({
...opts,
query,
variables,
cacheOptions,
preferredBehaviour,
unauthorizedHandler,
}));
return result;
}
export const generateFetchAppLogUrl = async (cursor, filters) => {
const fqdn = await partnersFqdn();
const url = `https://${fqdn}/app_logs/poll`;
return addCursorAndFiltersToAppLogsUrl(url, cursor, filters);
};
/**
* Executes a GraphQL query against the Partners API. Uses typed documents.
*
* @param query - GraphQL query to execute.
* @param token - Partners token.
* @param variables - GraphQL variables to pass to the query.
* @param preferredBehaviour - Preferred behaviour for the request.
* @param unauthorizedHandler - Optional handler for unauthorized requests.
* @returns The response of the query of generic type <TResult>.
*/
export async function partnersRequestDoc(query, token, variables, preferredBehaviour, unauthorizedHandler) {
try {
const opts = await setupRequest(token);
const result = limiter.schedule(() => graphqlRequestDoc({
...opts,
query,
variables,
preferredBehaviour,
unauthorizedHandler,
}));
return result;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
if (error.errors?.[0]?.extensions?.type === 'unsupported_client_version') {
const packageManager = await getPackageManager(cwd());
throw new AbortError(['Upgrade your CLI version to run this command.'], null, [
['Run', { command: formatPackageManagerCommand(packageManager, 'shopify upgrade') }],
]);
}
throw error;
}
}
/**
* 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=partners.js.map