MCPcopy
hub / github.com/sindresorhus/got / create

Function create

source/create.ts:61–396  ·  view source on GitHub ↗
(defaults: InstanceDefaults)

Source from the content-addressed store, hash-verified

59};
60
61const create = (defaults: InstanceDefaults): Got => {
62 defaults = {
63 options: new Options(undefined, undefined, defaults.options),
64 handlers: [...defaults.handlers],
65 mutableDefaults: defaults.mutableDefaults,
66 };
67
68 Object.defineProperty(defaults, 'mutableDefaults', {
69 enumerable: true,
70 configurable: false,
71 writable: false,
72 });
73
74 const makeRequest = (url: string | URL | OptionsInit | undefined, options: OptionsInit | undefined, defaultOptions: Options, isStream: boolean): GotReturn => {
75 if (is.plainObject(url)) {
76 assertNoUrlInOptionsObject(url);
77 }
78
79 if (is.plainObject(options)) {
80 assertNoUrlInOptionsObject(options);
81 }
82
83 // `isStream` is skipped by `merge()`, so set it via the direct setter after construction.
84 // Avoid a synthetic second merge only for the single-options-object stream form.
85 const requestUrl = isStream && is.plainObject(url) ? cloneWithProperty(url, 'isStream', true) : url;
86 const requestOptions = isStream && !is.plainObject(url) && options ? cloneWithProperty(options, 'isStream', true) : options;
87
88 const request = new Request(requestUrl, requestOptions, defaultOptions);
89
90 if (isStream && request.options) {
91 request.options.isStream = true;
92 }
93
94 let promise: RequestPromise | undefined;
95 const urlBeforeHandlers = request.options?.url instanceof URL ? new URL(request.options.url) : undefined;
96 const boundaryBeforeHandlers = request.options ? getUrlPrefixBoundary(request.options) : undefined;
97
98 const lastHandler = (normalized: Options): GotReturn => {
99 if (
100 urlBeforeHandlers
101 && boundaryBeforeHandlers
102 && normalized?.url instanceof URL
103 && hasUrlOrPrefixUrlBoundaryChanged(normalized, normalized.url, boundaryBeforeHandlers)
104 ) {
105 assertUrlHasSameOriginAsPrefixUrlIfNeeded(normalized, normalized.url);
106 }
107
108 // Note: `options` is `undefined` when `new Options(...)` fails
109 request.options = normalized;
110 const shouldReturnStream = normalized?.isStream ?? isStream;
111 request._noPipe = !shouldReturnStream;
112 void request.flush();
113
114 if (shouldReturnStream) {
115 return request;
116 }
117
118 promise ??= asPromise(request);

Callers 1

index.tsFile · 0.85

Calls 15

mergeMethod · 0.95
isGotInstanceFunction · 0.85
delayFunction · 0.85
getUrlPrefixBoundaryFunction · 0.85
snapshotCrossOriginStateFunction · 0.85
isSameOriginFunction · 0.85
applyUrlOverrideFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…