@native-marketing/sdk 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/http.js +75 -3
- package/package.json +1 -1
- package/release.json +2 -2
- package/types/http/request-policy.d.ts +5 -0
package/dist/http.js
CHANGED
|
@@ -1,11 +1,83 @@
|
|
|
1
1
|
// packages/api-client/src/client.ts
|
|
2
2
|
import createClient from "openapi-fetch";
|
|
3
|
+
|
|
4
|
+
// packages/api-client/src/request-policy.ts
|
|
5
|
+
var verbs = /* @__PURE__ */ new Set(["GET", "PUT", "POST", "DELETE", "OPTIONS", "HEAD", "PATCH", "TRACE"]);
|
|
6
|
+
function unsafe() {
|
|
7
|
+
throw new TypeError("HTTP client request violates its configured secure API boundary.");
|
|
8
|
+
}
|
|
9
|
+
function scopeOf(value) {
|
|
10
|
+
if (typeof value !== "string" || value.trim() !== value || /[\\?#]/.test(value)) unsafe();
|
|
11
|
+
const relative = value.startsWith("/") && !value.startsWith("//");
|
|
12
|
+
let url;
|
|
13
|
+
try {
|
|
14
|
+
url = relative ? new URL(value, "https://relative.invalid") : new URL(value);
|
|
15
|
+
} catch {
|
|
16
|
+
return unsafe();
|
|
17
|
+
}
|
|
18
|
+
if (url.username || url.password || url.search || url.hash) unsafe();
|
|
19
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)))
|
|
20
|
+
unsafe();
|
|
21
|
+
return {
|
|
22
|
+
...relative ? {} : { origin: url.origin },
|
|
23
|
+
pathname: url.pathname.replace(/\/+$/, "")
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function httpRequestPolicy(baseUrl, transport) {
|
|
27
|
+
const scope = scopeOf(baseUrl);
|
|
28
|
+
const origin = scope.origin ?? globalThis.location?.origin;
|
|
29
|
+
return {
|
|
30
|
+
validateInit(value) {
|
|
31
|
+
if (value === void 0) return;
|
|
32
|
+
if (value === null || typeof value !== "object") unsafe();
|
|
33
|
+
const init = value;
|
|
34
|
+
if (init.redirect !== void 0 && init.redirect !== "error") unsafe();
|
|
35
|
+
if (init.fetch !== void 0 || init.Request !== void 0 || init.middleware !== void 0)
|
|
36
|
+
unsafe();
|
|
37
|
+
if (init.baseUrl !== void 0) {
|
|
38
|
+
if (typeof init.baseUrl !== "string") unsafe();
|
|
39
|
+
const alternate = scopeOf(init.baseUrl);
|
|
40
|
+
if ((alternate.origin ?? origin) !== origin || alternate.pathname !== scope.pathname)
|
|
41
|
+
unsafe();
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
async fetch(request) {
|
|
45
|
+
const url = new URL(request.url);
|
|
46
|
+
if (!origin || url.origin !== origin || url.username || url.password || !(url.pathname === scope.pathname || url.pathname.startsWith(`${scope.pathname}/`)) || request.redirect !== "error")
|
|
47
|
+
unsafe();
|
|
48
|
+
return transport(request);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function guardHttpMethods(client, validate) {
|
|
53
|
+
const methods = /* @__PURE__ */ new Map();
|
|
54
|
+
return new Proxy(client, {
|
|
55
|
+
get(target, property, receiver) {
|
|
56
|
+
const value = Reflect.get(target, property, receiver);
|
|
57
|
+
if (typeof property !== "string" || typeof value !== "function" || !(verbs.has(property) || property === "request"))
|
|
58
|
+
return value;
|
|
59
|
+
if (!methods.has(property))
|
|
60
|
+
methods.set(property, async (...args) => {
|
|
61
|
+
validate(args[property === "request" ? 2 : 1]);
|
|
62
|
+
return Reflect.apply(value, target, args);
|
|
63
|
+
});
|
|
64
|
+
return methods.get(property);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// packages/api-client/src/client.ts
|
|
3
70
|
function createContentFactoryClient(options = {}) {
|
|
71
|
+
const baseUrl = options.baseUrl ?? "/api/v1";
|
|
72
|
+
const policy = httpRequestPolicy(
|
|
73
|
+
baseUrl,
|
|
74
|
+
options.fetch ?? ((request) => globalThis.fetch(request))
|
|
75
|
+
);
|
|
4
76
|
const client = createClient({
|
|
5
|
-
baseUrl
|
|
77
|
+
baseUrl,
|
|
6
78
|
credentials: "include",
|
|
7
79
|
redirect: "error",
|
|
8
|
-
|
|
80
|
+
fetch: policy.fetch
|
|
9
81
|
});
|
|
10
82
|
client.use({
|
|
11
83
|
onRequest({ request }) {
|
|
@@ -16,7 +88,7 @@ function createContentFactoryClient(options = {}) {
|
|
|
16
88
|
return new Request(request, { headers, body: "{}" });
|
|
17
89
|
}
|
|
18
90
|
});
|
|
19
|
-
return client;
|
|
91
|
+
return guardHttpMethods(client, policy.validateInit);
|
|
20
92
|
}
|
|
21
93
|
|
|
22
94
|
// packages/contracts/src/business-code.ts
|
package/package.json
CHANGED
package/release.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"sourceCommit": "
|
|
2
|
+
"sourceCommit": "09d271148a0cdfdfa89b0404023cc24a55c42618",
|
|
3
3
|
"workingTreeDirty": false,
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.2",
|
|
5
5
|
"openapiTypesSha256": "1a86e24ce9e458e945e4df5e9c7423c9bf4253ac24b3f627f67ba38c92856a38",
|
|
6
6
|
"localBuild": true
|
|
7
7
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function httpRequestPolicy(baseUrl: string, transport: (input: Request) => Promise<Response>): {
|
|
2
|
+
validateInit(value: unknown): void;
|
|
3
|
+
fetch(request: Request): Promise<Response>;
|
|
4
|
+
};
|
|
5
|
+
export declare function guardHttpMethods<T extends object>(client: T, validate: (init: unknown) => void): T;
|