1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ExpoRequest = exports.ExpoURL = exports.NON_STANDARD_SYMBOL = exports.ExpoResponse = exports.installGlobals = void 0;
4const node_1 = require("@remix-run/node");
5const node_url_1 = require("node:url");
6// Ensure these are available for the API Routes.
7function installGlobals() {
8    (0, node_1.installGlobals)();
9    // @ts-expect-error
10    global.Request = ExpoRequest;
11    // @ts-expect-error
12    global.Response = ExpoResponse;
13    // @ts-expect-error
14    global.ExpoResponse = ExpoResponse;
15    // @ts-expect-error
16    global.ExpoRequest = ExpoRequest;
17}
18exports.installGlobals = installGlobals;
19class ExpoResponse extends node_1.Response {
20    // TODO: Drop when we upgrade to node-fetch v3
21    static json(data = undefined, init = {}) {
22        const body = JSON.stringify(data);
23        if (body === undefined) {
24            throw new TypeError('data is not JSON serializable');
25        }
26        const headers = new node_1.Headers(init?.headers);
27        if (!headers.has('content-type')) {
28            headers.set('content-type', 'application/json');
29        }
30        return new ExpoResponse(body, {
31            ...init,
32            headers,
33        });
34    }
35}
36exports.ExpoResponse = ExpoResponse;
37exports.NON_STANDARD_SYMBOL = Symbol('non-standard');
38class ExpoURL extends node_url_1.URL {
39    static from(url, config) {
40        const expoUrl = new ExpoURL(url);
41        const match = config.namedRegex.exec(expoUrl.pathname);
42        if (match?.groups) {
43            for (const [key, value] of Object.entries(match.groups)) {
44                const namedKey = config.routeKeys[key];
45                expoUrl.searchParams.set(namedKey, value);
46            }
47        }
48        return expoUrl;
49    }
50}
51exports.ExpoURL = ExpoURL;
52class ExpoRequest extends node_1.Request {
53    constructor(info, init) {
54        super(info, init);
55        this[exports.NON_STANDARD_SYMBOL] = {
56            url: new ExpoURL(typeof info !== 'string' && 'url' in info ? info.url : String(info)),
57        };
58    }
59    get expoUrl() {
60        return this[exports.NON_STANDARD_SYMBOL].url;
61    }
62}
63exports.ExpoRequest = ExpoRequest;
64//# sourceMappingURL=environment.js.map