MCPcopy Index your code
hub / github.com/tensorflow/tfjs / fetch

Function fetch

tfjs-react-native/src/platform_react_native.ts:73–127  ·  view source on GitHub ↗
(
  path: string,
  init?: RequestInit,
  options?: tf.io.RequestDetails
)

Source from the content-addressed store, hash-verified

71 * @doc {heading: 'Platform helpers', subheading: 'http'}
72 */
73export async function fetch(
74 path: string,
75 init?: RequestInit,
76 options?: tf.io.RequestDetails
77): Promise<Response> {
78 return new Promise((resolve, reject) => {
79 const request = new Request(path, init);
80 const xhr = new XMLHttpRequest();
81
82 xhr.onload = () => {
83 const reqOptions = {
84 status: xhr.status,
85 statusText: xhr.statusText,
86 headers: parseHeaders(xhr.getAllResponseHeaders() || ""),
87 url: "",
88 };
89 reqOptions.url =
90 "responseURL" in xhr
91 ? xhr.responseURL
92 : reqOptions.headers.get("X-Request-URL");
93
94 //@ts-ignore — ts believes the latter case will never occur.
95 const body = "response" in xhr ? xhr.response : xhr.responseText;
96
97 resolve(new Response(body, reqOptions));
98 };
99
100 xhr.onerror = () => reject(new TypeError("Network request failed"));
101 xhr.ontimeout = () => reject(new TypeError("Network request failed"));
102
103 xhr.open(request.method, request.url, true);
104
105 if (request.credentials === "include") {
106 xhr.withCredentials = true;
107 } else if (request.credentials === "omit") {
108 xhr.withCredentials = false;
109 }
110
111 if (options != null && options.isBinary) {
112 // In react native We need to set the response type to arraybuffer when
113 // fetching binary resources in order for `.arrayBuffer` to work correctly
114 // on the response.
115 xhr.responseType = "arraybuffer";
116 }
117
118 request.headers.forEach((value: string, name: string) => {
119 xhr.setRequestHeader(name, value);
120 });
121
122 xhr.send(
123 //@ts-ignore
124 typeof request._bodyInit === "undefined" ? null : request._bodyInit
125 );
126 });
127}
128
129export class PlatformReactNative implements Platform {
130 /**

Callers 3

loadViaHttpMethod · 0.90
componentDidMountMethod · 0.90
fetchMethod · 0.70

Calls 3

parseHeadersFunction · 0.85
getMethod · 0.45
openMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…