@ascendkit/nextjs 0.2.2 → 0.2.3
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.
|
@@ -9,7 +9,11 @@ export interface ConditionalGetResult<T> {
|
|
|
9
9
|
data?: T;
|
|
10
10
|
etag?: string;
|
|
11
11
|
}
|
|
12
|
-
export
|
|
12
|
+
export interface CreateHttpClientOptions extends HttpClientOptions {
|
|
13
|
+
/** Optional invocation ID for correlating multiple requests in a single operation. */
|
|
14
|
+
invocationId?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function createHttpClient({ publicKey, apiUrl, secretKey, invocationId }: CreateHttpClientOptions): {
|
|
13
17
|
post: <T = any>(path: string, body: unknown) => Promise<T>;
|
|
14
18
|
get: <T = any>(path: string) => Promise<T>;
|
|
15
19
|
getConditional: <T = any>(path: string, etag?: string) => Promise<ConditionalGetResult<T>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-client.d.ts","sourceRoot":"","sources":["../../src/shared/http-client.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,UAAU,CAAC;AAEnC,MAAM,WAAW,iBAAiB;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB,CAAC,CAAC;IACrC,WAAW,EAAE,OAAO,CAAC;IACrB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAgBD,wBAAgB,gBAAgB,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,
|
|
1
|
+
{"version":3,"file":"http-client.d.ts","sourceRoot":"","sources":["../../src/shared/http-client.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,UAAU,CAAC;AAEnC,MAAM,WAAW,iBAAiB;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB,CAAC,CAAC;IACrC,WAAW,EAAE,OAAO,CAAC;IACrB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAgBD,MAAM,WAAW,uBAAwB,SAAQ,iBAAiB;IAChE,sFAAsF;IACtF,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,wBAAgB,gBAAgB,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,uBAAuB;WA8BlF,CAAC,cAAc,MAAM,QAAQ,OAAO,KAAG,OAAO,CAAC,CAAC,CAAC;UASlD,CAAC,cAAc,MAAM,KAAG,OAAO,CAAC,CAAC,CAAC;qBAOvB,CAAC,cAAc,MAAM,SAAS,MAAM,KAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC;EA0BtG;AAED,MAAM,MAAM,UAAU,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC"}
|
|
@@ -9,14 +9,24 @@ function checkUpgradeHeader(res) {
|
|
|
9
9
|
upgradeWarned = true;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
export function createHttpClient({ publicKey, apiUrl, secretKey }) {
|
|
12
|
+
export function createHttpClient({ publicKey, apiUrl, secretKey, invocationId }) {
|
|
13
|
+
const instanceInvocationId = invocationId ?? crypto.randomUUID();
|
|
13
14
|
const baseHeaders = {
|
|
14
15
|
"X-AscendKit-Public-Key": publicKey,
|
|
15
16
|
"X-AscendKit-Client-Version": `js/${SDK_VERSION}`,
|
|
17
|
+
"X-AscendKit-Invocation-Id": instanceInvocationId,
|
|
16
18
|
};
|
|
17
19
|
if (secretKey) {
|
|
18
20
|
baseHeaders["X-AscendKit-Secret-Key"] = secretKey;
|
|
19
21
|
}
|
|
22
|
+
/** Returns per-request headers including a fresh client request ID. */
|
|
23
|
+
function requestHeaders(extra) {
|
|
24
|
+
return {
|
|
25
|
+
...baseHeaders,
|
|
26
|
+
"X-AscendKit-Client-Request-Id": crypto.randomUUID(),
|
|
27
|
+
...extra,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
20
30
|
async function handleResponse(res) {
|
|
21
31
|
checkUpgradeHeader(res);
|
|
22
32
|
const json = await res.json();
|
|
@@ -28,24 +38,24 @@ export function createHttpClient({ publicKey, apiUrl, secretKey }) {
|
|
|
28
38
|
async function post(path, body) {
|
|
29
39
|
const res = await fetch(`${apiUrl}${path}`, {
|
|
30
40
|
method: "POST",
|
|
31
|
-
headers: {
|
|
41
|
+
headers: requestHeaders({ "Content-Type": "application/json" }),
|
|
32
42
|
body: JSON.stringify(body),
|
|
33
43
|
});
|
|
34
44
|
return handleResponse(res);
|
|
35
45
|
}
|
|
36
46
|
async function get(path) {
|
|
37
47
|
const res = await fetch(`${apiUrl}${path}`, {
|
|
38
|
-
headers:
|
|
48
|
+
headers: requestHeaders(),
|
|
39
49
|
});
|
|
40
50
|
return handleResponse(res);
|
|
41
51
|
}
|
|
42
52
|
async function getConditional(path, etag) {
|
|
43
|
-
const
|
|
53
|
+
const extra = {};
|
|
44
54
|
if (etag) {
|
|
45
|
-
|
|
55
|
+
extra["If-None-Match"] = etag;
|
|
46
56
|
}
|
|
47
57
|
const res = await fetch(`${apiUrl}${path}`, {
|
|
48
|
-
headers,
|
|
58
|
+
headers: requestHeaders(extra),
|
|
49
59
|
cache: "no-store",
|
|
50
60
|
});
|
|
51
61
|
if (res.status === 304) {
|