@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
86 lines (85 loc) • 4.17 kB
TypeScript
import { NetworkRetryBehaviour } from '../../private/node/api.js';
import FormData from 'form-data';
import { RequestInfo, RequestInit, Response } from 'node-fetch';
export { FetchError, Request, Response } from 'node-fetch';
/**
* Create a new FormData object.
*
* @returns A FormData object.
*/
export declare function formData(): FormData;
type AbortSignal = RequestInit['signal'];
type PresetFetchBehaviour = 'default' | 'non-blocking' | 'slow-request';
type AutomaticCancellationBehaviour = {
useAbortSignal: true;
timeoutMs: number;
} | {
useAbortSignal: false;
} | {
useAbortSignal: AbortSignal | (() => AbortSignal);
};
type RequestBehaviour = NetworkRetryBehaviour & AutomaticCancellationBehaviour;
export type RequestModeInput = PresetFetchBehaviour | RequestBehaviour;
/**
* Specify the behaviour of a network request.
*
* - default: Requests are automatically retried, and are subject to automatic cancellation if they're taking too long.
* This is generally desirable.
* - non-blocking: Requests are not retried if they fail with a network error, and are automatically cancelled if
* they're taking too long. This is good for throwaway requests, like polling or tracking.
* - slow-request: Requests are not retried if they fail with a network error, and are not automatically cancelled.
* This is good for slow requests that should be give the chance to complete, and are unlikely to be safe to retry.
*
* Some request behaviours may be de-activated by the environment, and this function takes care of that concern. You
* can also provide a customised request behaviour.
*
* @param preset - The preset to use.
* @param env - Process environment variables.
* @returns A request behaviour object.
*/
export declare function requestMode(preset?: RequestModeInput, env?: NodeJS.ProcessEnv): RequestBehaviour;
/**
* Create an AbortSignal for automatic request cancellation, from a request behaviour.
*
* @param behaviour - The request behaviour.
* @returns An AbortSignal.
*/
export declare function abortSignalFromRequestBehaviour(behaviour: RequestBehaviour): AbortSignal;
/**
* An interface that abstracts way node-fetch. When Node has built-in
* support for "fetch" in the standard library, we can drop the node-fetch
* dependency from here.
* Note that we are exposing types from "node-fetch". The reason being is that
* they are consistent with the Web API so if we drop node-fetch in the future
* it won't require changes from the callers.
*
* The CLI's fetch function supports special behaviours, like automatic retries. These are disabled by default through
* this function.
*
* @param url - This defines the resource that you wish to fetch.
* @param init - An object containing any custom settings that you want to apply to the request.
* @param preferredBehaviour - A request behaviour object that overrides the default behaviour.
* @returns A promise that resolves with the response.
*/
export declare function fetch(url: RequestInfo, init?: RequestInit, preferredBehaviour?: RequestModeInput): Promise<Response>;
/**
* A fetch function to use with Shopify services. The function ensures the right
* TLS configuragion is used based on the environment in which the service is running
* (e.g. Spin). NB: headers/auth are the responsibility of the caller.
*
* By default, the CLI's fetch function's special behaviours, like automatic retries, are enabled.
*
* @param url - This defines the resource that you wish to fetch.
* @param init - An object containing any custom settings that you want to apply to the request.
* @param preferredBehaviour - A request behaviour object that overrides the default behaviour.
* @returns A promise that resolves with the response.
*/
export declare function shopifyFetch(url: RequestInfo, init?: RequestInit, preferredBehaviour?: RequestModeInput): Promise<Response>;
/**
* Download a file from a URL to a local path.
*
* @param url - The URL to download from.
* @param to - The local path to download to.
* @returns - A promise that resolves with the local path.
*/
export declare function downloadFile(url: string, to: string): Promise<string>;