@encore.dev/frontend 0.0.0-placeholder.0 → 2.0.0-beta.16
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/LICENSE +362 -0
- package/README.md +18 -1
- package/client.ts +158 -0
- package/config.ts +31 -0
- package/dist/client.d.ts +76 -0
- package/dist/client.js +105 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +17 -0
- package/dist/config.js +28 -0
- package/dist/config.js.map +1 -0
- package/dist/graphql-ws.d.ts +35 -0
- package/dist/graphql-ws.js +130 -0
- package/dist/graphql-ws.js.map +1 -0
- package/dist/graphql.d.ts +16 -0
- package/dist/graphql.js +14 -0
- package/dist/graphql.js.map +1 -0
- package/dist/mod.d.ts +16 -0
- package/dist/mod.js +13 -0
- package/dist/mod.js.map +1 -0
- package/dist/sse.d.ts +25 -0
- package/dist/sse.js +101 -0
- package/dist/sse.js.map +1 -0
- package/graphql-ws.ts +160 -0
- package/graphql.ts +20 -0
- package/mod.ts +26 -0
- package/package.json +38 -6
- package/sse.ts +107 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared runtime for generated Gateway Clients, handling request construction,
|
|
3
|
+
* header and auth merging, and error handling. Generated code contains only
|
|
4
|
+
* per-app types, methods, and environment resolution.
|
|
5
|
+
*/
|
|
6
|
+
/** Error thrown when the gateway responds with a non-2xx status. */
|
|
7
|
+
export class APIError extends Error {
|
|
8
|
+
status;
|
|
9
|
+
body;
|
|
10
|
+
constructor(status, body) {
|
|
11
|
+
super(`api error: status ${status}: ${body}`);
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.body = body;
|
|
14
|
+
this.name = "APIError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Shared HTTP transport used by generated Gateway Clients. A gateway's
|
|
19
|
+
* per-service clients share a single instance so base URL and auth
|
|
20
|
+
* configuration are resolved once.
|
|
21
|
+
*/
|
|
22
|
+
export class CallClient {
|
|
23
|
+
baseURL;
|
|
24
|
+
#fetcher;
|
|
25
|
+
#headers;
|
|
26
|
+
#auth;
|
|
27
|
+
/** `baseURL` must already be resolved (see `resolveBaseURL`). */
|
|
28
|
+
constructor(baseURL, options) {
|
|
29
|
+
this.baseURL = baseURL.replace(/\/+$/, "");
|
|
30
|
+
// A detached `fetch` reference loses its receiver binding in browsers.
|
|
31
|
+
this.#fetcher = options?.fetcher ?? ((input, init) => globalThis.fetch(input, init));
|
|
32
|
+
this.#headers = { ...options?.headers };
|
|
33
|
+
this.#auth = options?.auth;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Performs the request and returns the raw `Response`. Throws `APIError`
|
|
37
|
+
* on any non-2xx status.
|
|
38
|
+
*/
|
|
39
|
+
async request(spec) {
|
|
40
|
+
const resp = await this.requestRaw(spec);
|
|
41
|
+
if (!resp.ok) {
|
|
42
|
+
throw new APIError(resp.status, await resp.text());
|
|
43
|
+
}
|
|
44
|
+
return resp;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Performs the request and returns the raw `Response` regardless of status
|
|
48
|
+
* code. Useful for protocols like GraphQL that return structured errors in
|
|
49
|
+
* response bodies.
|
|
50
|
+
*/
|
|
51
|
+
async requestRaw(spec) {
|
|
52
|
+
let url = this.baseURL + spec.path;
|
|
53
|
+
if (spec.query !== undefined) {
|
|
54
|
+
const params = new URLSearchParams();
|
|
55
|
+
for (const [key, value] of Object.entries(spec.query)) {
|
|
56
|
+
if (value === undefined || value === null)
|
|
57
|
+
continue;
|
|
58
|
+
for (const v of Array.isArray(value) ? value : [value]) {
|
|
59
|
+
params.append(key, String(v));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const qs = params.toString();
|
|
63
|
+
if (qs !== "")
|
|
64
|
+
url += `?${qs}`;
|
|
65
|
+
}
|
|
66
|
+
// Header names are case-insensitive, so a later source's header
|
|
67
|
+
// replaces an earlier one's whatever the case of either.
|
|
68
|
+
const headers = new Headers();
|
|
69
|
+
for (const [key, value] of Object.entries(this.#headers)) {
|
|
70
|
+
headers.set(key, value);
|
|
71
|
+
}
|
|
72
|
+
if (this.#auth !== undefined) {
|
|
73
|
+
for (const [key, value] of Object.entries(await this.#auth())) {
|
|
74
|
+
headers.set(key, value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const [key, value] of Object.entries(spec.headers ?? {})) {
|
|
78
|
+
if (value !== undefined)
|
|
79
|
+
headers.set(key, value);
|
|
80
|
+
}
|
|
81
|
+
let body = spec.body;
|
|
82
|
+
if (spec.json !== undefined) {
|
|
83
|
+
body = JSON.stringify(spec.json);
|
|
84
|
+
if (!headers.has("Content-Type"))
|
|
85
|
+
headers.set("Content-Type", "application/json");
|
|
86
|
+
}
|
|
87
|
+
return await this.#fetcher(url, { method: spec.method, headers, body });
|
|
88
|
+
}
|
|
89
|
+
/** Performs the request and decodes the JSON response body. */
|
|
90
|
+
async json(spec) {
|
|
91
|
+
const resp = await this.request(spec);
|
|
92
|
+
return (await resp.json());
|
|
93
|
+
}
|
|
94
|
+
/** Performs the request and returns the response body as text. */
|
|
95
|
+
async text(spec) {
|
|
96
|
+
const resp = await this.request(spec);
|
|
97
|
+
return await resp.text();
|
|
98
|
+
}
|
|
99
|
+
/** Performs the request and returns the response body as bytes. */
|
|
100
|
+
async binary(spec) {
|
|
101
|
+
const resp = await this.request(spec);
|
|
102
|
+
return new Uint8Array(await resp.arrayBuffer());
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA2BH,oEAAoE;AACpE,MAAM,OAAO,QAAS,SAAQ,KAAK;IAEf;IACA;IAFlB,YACkB,MAAc,EACd,IAAY;QAE5B,KAAK,CAAC,qBAAqB,MAAM,KAA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
|
|
2
|
+
* Gateway base URL handling.
|
|
3
|
+
*
|
|
4
|
+
* A generated Gateway Client is given its gateway's base URL as the `baseURL`
|
|
5
|
+
* client option, from wherever the app has it (such as a bundler environment
|
|
6
|
+
* variable Encore sets, a config endpoint, or `window.location.origin`), e.g.:
|
|
7
|
+
* `new Client({ baseURL: import.meta.env.VITE_ENCORE_GATEWAY_URL_API })`.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Checks a Gateway Client's `baseURL` option and returns it without trailing
|
|
11
|
+
* slashes. Throws if it is missing or empty, naming the gateway, for callers
|
|
12
|
+
* the type checker does not reach (plain JavaScript, a variable not set).
|
|
13
|
+
*
|
|
14
|
+
* @param gateway - The gateway name, e.g. `"api"`.
|
|
15
|
+
* @param baseURL - The client's `baseURL` option.
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveBaseURL(gateway: string, baseURL: string | undefined): string;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway base URL handling.
|
|
3
|
+
*
|
|
4
|
+
* A generated Gateway Client is given its gateway's base URL as the `baseURL`
|
|
5
|
+
* client option, from wherever the app has it (such as a bundler environment
|
|
6
|
+
* variable Encore sets, a config endpoint, or `window.location.origin`), e.g.:
|
|
7
|
+
* `new Client({ baseURL: import.meta.env.VITE_ENCORE_GATEWAY_URL_API })`.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Checks a Gateway Client's `baseURL` option and returns it without trailing
|
|
11
|
+
* slashes. Throws if it is missing or empty, naming the gateway, for callers
|
|
12
|
+
* the type checker does not reach (plain JavaScript, a variable not set).
|
|
13
|
+
*
|
|
14
|
+
* @param gateway - The gateway name, e.g. `"api"`.
|
|
15
|
+
* @param baseURL - The client's `baseURL` option.
|
|
16
|
+
*/
|
|
17
|
+
export function resolveBaseURL(gateway, baseURL) {
|
|
18
|
+
if (baseURL === undefined || baseURL === "") {
|
|
19
|
+
throw new Error(`no base URL for gateway "${gateway}": pass baseURL in the client options, ` +
|
|
20
|
+
`e.g. new Client({ baseURL: import.meta.env.VITE_ENCORE_GATEWAY_URL_${gatewayVarName(gateway)} })`);
|
|
21
|
+
}
|
|
22
|
+
return baseURL.replace(/\/+$/, "");
|
|
23
|
+
}
|
|
24
|
+
/** The gateway's part of its URL variable's name: uppercased, `-` as `_` (ADR 0026). */
|
|
25
|
+
function gatewayVarName(gateway) {
|
|
26
|
+
return gateway.replace(/-/g, "_").toUpperCase();
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../config.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,OAAe,EAAE,OAA2B;IACzE,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,EAAE,EAAE,CAAC;QAC5C,MAAM,IAAI,KAAK,CACb,4BAA4B,OAAO,yCAAyC;YAC1E,sEAAsE,cAAc,CAAC,OAAO,CAAC,KAAK,CACrG,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACrC,CAAC;AAED,wFAAwF;AACxF,SAAS,cAAc,CAAC,OAAe;IACrC,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal `graphql-transport-ws` client used by generated GraphQL subscription
|
|
3
|
+
* methods. Opens one WebSocket per subscription with zero external dependencies.
|
|
4
|
+
*/
|
|
5
|
+
import type { GraphQLError } from "./graphql.js";
|
|
6
|
+
/** A GraphQL request payload. */
|
|
7
|
+
export interface GraphQLRequest {
|
|
8
|
+
query: string;
|
|
9
|
+
variables?: Record<string, unknown>;
|
|
10
|
+
operationName?: string;
|
|
11
|
+
}
|
|
12
|
+
/** Error thrown when a GraphQL subscription fails or receives server-side errors. */
|
|
13
|
+
export declare class GraphQLSubscriptionError extends Error {
|
|
14
|
+
readonly errors: GraphQLError[];
|
|
15
|
+
constructor(errors: GraphQLError[]);
|
|
16
|
+
}
|
|
17
|
+
export interface SubscribeOptions {
|
|
18
|
+
/** The WebSocket URL of the GraphQL endpoint (`ws://` or `wss://`). */
|
|
19
|
+
url: string;
|
|
20
|
+
/** Payload sent with `connection_init` (such as auth credentials), evaluated per connection. */
|
|
21
|
+
connectionParams?: () => Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
22
|
+
/** Overrides the WebSocket implementation (defaults to the global `WebSocket`). */
|
|
23
|
+
webSocket?: typeof WebSocket;
|
|
24
|
+
}
|
|
25
|
+
/** The `graphql-transport-ws` subprotocol name. */
|
|
26
|
+
export declare const GRAPHQL_TRANSPORT_WS = "graphql-transport-ws";
|
|
27
|
+
/**
|
|
28
|
+
* Executes `request` as a GraphQL subscription over `graphql-transport-ws`,
|
|
29
|
+
* yielding `data` from each `next` message. Completes when the server ends the
|
|
30
|
+
* subscription, and throws `GraphQLSubscriptionError` on GraphQL or protocol
|
|
31
|
+
* errors. Breaking out of the loop sends `complete` and closes the socket.
|
|
32
|
+
*/
|
|
33
|
+
export declare function graphqlSubscribe<T = unknown>(opts: SubscribeOptions, request: GraphQLRequest): AsyncGenerator<T, void, undefined>;
|
|
34
|
+
/** Derives the WebSocket URL for a gateway path from an HTTP base URL. */
|
|
35
|
+
export declare function websocketURL(baseURL: string, path: string): string;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal `graphql-transport-ws` client used by generated GraphQL subscription
|
|
3
|
+
* methods. Opens one WebSocket per subscription with zero external dependencies.
|
|
4
|
+
*/
|
|
5
|
+
/** Error thrown when a GraphQL subscription fails or receives server-side errors. */
|
|
6
|
+
export class GraphQLSubscriptionError extends Error {
|
|
7
|
+
errors;
|
|
8
|
+
constructor(errors) {
|
|
9
|
+
super(errors.map((e) => e.message).join("; ") || "subscription error");
|
|
10
|
+
this.errors = errors;
|
|
11
|
+
this.name = "GraphQLSubscriptionError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** The `graphql-transport-ws` subprotocol name. */
|
|
15
|
+
export const GRAPHQL_TRANSPORT_WS = "graphql-transport-ws";
|
|
16
|
+
/**
|
|
17
|
+
* Executes `request` as a GraphQL subscription over `graphql-transport-ws`,
|
|
18
|
+
* yielding `data` from each `next` message. Completes when the server ends the
|
|
19
|
+
* subscription, and throws `GraphQLSubscriptionError` on GraphQL or protocol
|
|
20
|
+
* errors. Breaking out of the loop sends `complete` and closes the socket.
|
|
21
|
+
*/
|
|
22
|
+
export async function* graphqlSubscribe(opts, request) {
|
|
23
|
+
const WS = opts.webSocket ?? globalThis.WebSocket;
|
|
24
|
+
if (WS === undefined) {
|
|
25
|
+
throw new Error("no WebSocket implementation available; pass webSocket in the options");
|
|
26
|
+
}
|
|
27
|
+
const socket = new WS(opts.url, GRAPHQL_TRANSPORT_WS);
|
|
28
|
+
const id = "1"; // one subscription per socket
|
|
29
|
+
// Pump socket events into a pull queue so the generator can await them.
|
|
30
|
+
const queue = [];
|
|
31
|
+
let notify;
|
|
32
|
+
let closed;
|
|
33
|
+
let socketError;
|
|
34
|
+
const wake = () => {
|
|
35
|
+
notify?.();
|
|
36
|
+
notify = undefined;
|
|
37
|
+
};
|
|
38
|
+
socket.onmessage = (ev) => {
|
|
39
|
+
queue.push(JSON.parse(String(ev.data)));
|
|
40
|
+
wake();
|
|
41
|
+
};
|
|
42
|
+
socket.onerror = () => {
|
|
43
|
+
socketError = new Error("WebSocket error");
|
|
44
|
+
wake();
|
|
45
|
+
};
|
|
46
|
+
socket.onclose = (ev) => {
|
|
47
|
+
closed = { code: ev.code, reason: ev.reason };
|
|
48
|
+
wake();
|
|
49
|
+
};
|
|
50
|
+
const next = async () => {
|
|
51
|
+
for (;;) {
|
|
52
|
+
const msg = queue.shift();
|
|
53
|
+
if (msg !== undefined)
|
|
54
|
+
return msg;
|
|
55
|
+
if (socketError !== undefined)
|
|
56
|
+
throw socketError;
|
|
57
|
+
if (closed !== undefined) {
|
|
58
|
+
throw new Error(`WebSocket closed (${closed.code}${closed.reason ? `: ${closed.reason}` : ""})`);
|
|
59
|
+
}
|
|
60
|
+
await new Promise((resolve) => {
|
|
61
|
+
notify = resolve;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const send = (msg) => socket.send(JSON.stringify(msg));
|
|
66
|
+
try {
|
|
67
|
+
await new Promise((resolve, reject) => {
|
|
68
|
+
socket.onopen = () => resolve();
|
|
69
|
+
const prevError = socket.onerror;
|
|
70
|
+
socket.onerror = (ev) => {
|
|
71
|
+
reject(new Error("WebSocket connection failed"));
|
|
72
|
+
prevError?.call(socket, ev);
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
const payload = opts.connectionParams === undefined ? undefined : await opts.connectionParams();
|
|
76
|
+
send({ type: "connection_init", ...(payload !== undefined ? { payload } : {}) });
|
|
77
|
+
for (;;) {
|
|
78
|
+
const msg = await next();
|
|
79
|
+
if (msg.type === "connection_ack")
|
|
80
|
+
break;
|
|
81
|
+
if (msg.type === "ping")
|
|
82
|
+
send({ type: "pong" });
|
|
83
|
+
}
|
|
84
|
+
send({ type: "subscribe", id, payload: request });
|
|
85
|
+
for (;;) {
|
|
86
|
+
const msg = await next();
|
|
87
|
+
switch (msg.type) {
|
|
88
|
+
case "next":
|
|
89
|
+
if (msg.id !== id)
|
|
90
|
+
continue;
|
|
91
|
+
if (msg.payload.errors !== undefined && msg.payload.errors.length > 0) {
|
|
92
|
+
throw new GraphQLSubscriptionError(msg.payload.errors);
|
|
93
|
+
}
|
|
94
|
+
yield msg.payload.data;
|
|
95
|
+
break;
|
|
96
|
+
case "error":
|
|
97
|
+
if (msg.id !== id)
|
|
98
|
+
continue;
|
|
99
|
+
throw new GraphQLSubscriptionError(msg.payload);
|
|
100
|
+
case "complete":
|
|
101
|
+
if (msg.id !== id)
|
|
102
|
+
continue;
|
|
103
|
+
return;
|
|
104
|
+
case "ping":
|
|
105
|
+
send({ type: "pong" });
|
|
106
|
+
break;
|
|
107
|
+
default:
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
if (closed === undefined) {
|
|
114
|
+
try {
|
|
115
|
+
send({ type: "complete", id });
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// Socket already failed; closing is all that's left.
|
|
119
|
+
}
|
|
120
|
+
socket.close(1000, "done");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Derives the WebSocket URL for a gateway path from an HTTP base URL. */
|
|
125
|
+
export function websocketURL(baseURL, path) {
|
|
126
|
+
const url = new URL(baseURL.replace(/\/+$/, "") + path);
|
|
127
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
128
|
+
return url.toString();
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=graphql-ws.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphql-ws.js","sourceRoot":"","sources":["../graphql-ws.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,qFAAqF;AACrF,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACrB;IAA5B,YAA4B,MAAsB;QAChD,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,oBAAoB,CAAC,CAAC;QAD7C,WAAM,GAAN,MAAM,CAAgB;QAEhD,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AAaD,mDAAmD;AACnD,MAAM,CAAC,MAAM,oBAAoB,GAAG,sB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CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;oBACvB,MAAM;gBACR;oBACE,MAAM;YACV,CAAC;QACH,CAAC;IACH,CAAC;YAAS,CAAC;QACT,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,IAAI,CAAC;gBACH,IAAI,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,CAAC;YACjC,CAAC;YAAC,MAAM,CAAC;gBACP,qDAAqD;YACvD,CAAC;YACD,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;AACH,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,YAAY,CAAC,OAAe,EAAE,IAAY;IACxD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;IACxD,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;IAC1D,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** GraphQL response errors, shared by the query and subscription paths. */
|
|
2
|
+
/** A single error entry from a GraphQL response. */
|
|
3
|
+
export interface GraphQLError {
|
|
4
|
+
message: string;
|
|
5
|
+
path?: (string | number)[];
|
|
6
|
+
extensions?: unknown;
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Error thrown when a GraphQL response contains one or more errors. Partial
|
|
11
|
+
* data is discarded.
|
|
12
|
+
*/
|
|
13
|
+
export declare class GraphQLRequestError extends Error {
|
|
14
|
+
readonly errors: GraphQLError[];
|
|
15
|
+
constructor(errors: GraphQLError[]);
|
|
16
|
+
}
|
package/dist/graphql.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** GraphQL response errors, shared by the query and subscription paths. */
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a GraphQL response contains one or more errors. Partial
|
|
4
|
+
* data is discarded.
|
|
5
|
+
*/
|
|
6
|
+
export class GraphQLRequestError extends Error {
|
|
7
|
+
errors;
|
|
8
|
+
constructor(errors) {
|
|
9
|
+
super("graphql: " + errors.map((e) => e.message).join("; "));
|
|
10
|
+
this.errors = errors;
|
|
11
|
+
this.name = "GraphQLRequestError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=graphql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphql.js","sourceRoot":"","sources":["../graphql.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAU3E;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAChB;IAA5B,YAA4B,MAAsB;QAChD,KAAK,CAAC,WAAW,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QADnC,WAAM,GAAN,MAAM,CAAgB;QAEhD,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF"}
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encore's frontend SDK.
|
|
3
|
+
*
|
|
4
|
+
* Provides the client runtime used by generated Gateway Clients
|
|
5
|
+
* (`encore_gen/gateway/<name>.ts`), in the browser and on the server.
|
|
6
|
+
* Pure TypeScript with no backend runtime dependencies.
|
|
7
|
+
*/
|
|
8
|
+
export { resolveBaseURL } from "./config.js";
|
|
9
|
+
export { APIError, CallClient } from "./client.js";
|
|
10
|
+
export type { AuthFn, ClientOptions, Fetcher, QueryValue, RequestSpec } from "./client.js";
|
|
11
|
+
export { sseEvents, sseStream } from "./sse.js";
|
|
12
|
+
export type { SSEEvent } from "./sse.js";
|
|
13
|
+
export { GraphQLRequestError } from "./graphql.js";
|
|
14
|
+
export type { GraphQLError } from "./graphql.js";
|
|
15
|
+
export { graphqlSubscribe, websocketURL, GraphQLSubscriptionError, GRAPHQL_TRANSPORT_WS, } from "./graphql-ws.js";
|
|
16
|
+
export type { GraphQLRequest, SubscribeOptions } from "./graphql-ws.js";
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encore's frontend SDK.
|
|
3
|
+
*
|
|
4
|
+
* Provides the client runtime used by generated Gateway Clients
|
|
5
|
+
* (`encore_gen/gateway/<name>.ts`), in the browser and on the server.
|
|
6
|
+
* Pure TypeScript with no backend runtime dependencies.
|
|
7
|
+
*/
|
|
8
|
+
export { resolveBaseURL } from "./config.js";
|
|
9
|
+
export { APIError, CallClient } from "./client.js";
|
|
10
|
+
export { sseEvents, sseStream } from "./sse.js";
|
|
11
|
+
export { GraphQLRequestError } from "./graphql.js";
|
|
12
|
+
export { graphqlSubscribe, websocketURL, GraphQLSubscriptionError, GRAPHQL_TRANSPORT_WS, } from "./graphql-ws.js";
|
|
13
|
+
//# sourceMappingURL=mod.js.map
|
package/dist/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGnD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAGhD,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAGnD,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,iBAAiB,CAAC"}
|
package/dist/sse.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-sent events (SSE) parser that turns a `text/event-stream` response
|
|
3
|
+
* into a typed async iterable. Used by generated streaming methods.
|
|
4
|
+
*/
|
|
5
|
+
/** A single parsed SSE event. */
|
|
6
|
+
export interface SSEEvent {
|
|
7
|
+
/** The `event:` field, if sent by the server. */
|
|
8
|
+
event?: string;
|
|
9
|
+
/** The concatenated `data:` lines. */
|
|
10
|
+
data: string;
|
|
11
|
+
/** The `id:` field, if sent by the server. */
|
|
12
|
+
id?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Parses a `text/event-stream` response body into SSE events according to the
|
|
16
|
+
* WHATWG EventSource format. Fields accumulate until a blank line dispatches
|
|
17
|
+
* an event, comment lines starting with `:` are ignored, and multiple `data:`
|
|
18
|
+
* lines are joined with newlines.
|
|
19
|
+
*/
|
|
20
|
+
export declare function sseEvents(resp: Response): AsyncGenerator<SSEEvent, void, undefined>;
|
|
21
|
+
/**
|
|
22
|
+
* Parses a `text/event-stream` response and yields each event's JSON-decoded
|
|
23
|
+
* `data` payload.
|
|
24
|
+
*/
|
|
25
|
+
export declare function sseStream<T = unknown>(resp: Response): AsyncGenerator<T, void, undefined>;
|
package/dist/sse.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-sent events (SSE) parser that turns a `text/event-stream` response
|
|
3
|
+
* into a typed async iterable. Used by generated streaming methods.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Parses a `text/event-stream` response body into SSE events according to the
|
|
7
|
+
* WHATWG EventSource format. Fields accumulate until a blank line dispatches
|
|
8
|
+
* an event, comment lines starting with `:` are ignored, and multiple `data:`
|
|
9
|
+
* lines are joined with newlines.
|
|
10
|
+
*/
|
|
11
|
+
export async function* sseEvents(resp) {
|
|
12
|
+
if (resp.body === null)
|
|
13
|
+
return;
|
|
14
|
+
const decoder = new TextDecoder();
|
|
15
|
+
let buffer = "";
|
|
16
|
+
// Whether the last line ended in a CR at the end of what was read: an LF
|
|
17
|
+
// read next is that CRLF's second half, not a line end of its own.
|
|
18
|
+
let afterCR = false;
|
|
19
|
+
let data = [];
|
|
20
|
+
let event;
|
|
21
|
+
let id;
|
|
22
|
+
const dispatch = function* () {
|
|
23
|
+
if (data.length === 0) {
|
|
24
|
+
event = undefined;
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
yield { event, data: data.join("\n"), id };
|
|
28
|
+
data = [];
|
|
29
|
+
event = undefined;
|
|
30
|
+
};
|
|
31
|
+
const handleLine = function* (line) {
|
|
32
|
+
if (line === "") {
|
|
33
|
+
yield* dispatch();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (line.startsWith(":"))
|
|
37
|
+
return; // comment
|
|
38
|
+
const colon = line.indexOf(":");
|
|
39
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
40
|
+
let value = colon === -1 ? "" : line.slice(colon + 1);
|
|
41
|
+
if (value.startsWith(" "))
|
|
42
|
+
value = value.slice(1);
|
|
43
|
+
switch (field) {
|
|
44
|
+
case "data":
|
|
45
|
+
data.push(value);
|
|
46
|
+
break;
|
|
47
|
+
case "event":
|
|
48
|
+
event = value;
|
|
49
|
+
break;
|
|
50
|
+
case "id":
|
|
51
|
+
if (!value.includes("\0"))
|
|
52
|
+
id = value;
|
|
53
|
+
break;
|
|
54
|
+
// "retry" configures EventSource reconnection; meaningless here.
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const reader = resp.body.getReader();
|
|
58
|
+
try {
|
|
59
|
+
for (;;) {
|
|
60
|
+
const { done, value } = await reader.read();
|
|
61
|
+
if (done)
|
|
62
|
+
break;
|
|
63
|
+
buffer += decoder.decode(value, { stream: true });
|
|
64
|
+
if (afterCR && buffer !== "") {
|
|
65
|
+
if (buffer.startsWith("\n"))
|
|
66
|
+
buffer = buffer.slice(1);
|
|
67
|
+
afterCR = false;
|
|
68
|
+
}
|
|
69
|
+
// Line endings per the spec: CRLF, CR, or LF.
|
|
70
|
+
for (;;) {
|
|
71
|
+
const match = /\r\n|\r|\n/.exec(buffer);
|
|
72
|
+
if (match === null)
|
|
73
|
+
break;
|
|
74
|
+
const line = buffer.slice(0, match.index);
|
|
75
|
+
buffer = buffer.slice(match.index + match[0].length);
|
|
76
|
+
afterCR = match[0] === "\r" && buffer === "";
|
|
77
|
+
yield* handleLine(line);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
buffer += decoder.decode();
|
|
81
|
+
if (buffer !== "")
|
|
82
|
+
yield* handleLine(buffer);
|
|
83
|
+
// A final event without a trailing blank line still dispatches.
|
|
84
|
+
yield* dispatch();
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
reader.releaseLock();
|
|
88
|
+
// Stop the transfer if the consumer broke out early.
|
|
89
|
+
await resp.body.cancel().catch(() => { });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Parses a `text/event-stream` response and yields each event's JSON-decoded
|
|
94
|
+
* `data` payload.
|
|
95
|
+
*/
|
|
96
|
+
export async function* sseStream(resp) {
|
|
97
|
+
for await (const event of sseEvents(resp)) {
|
|
98
|
+
yield JSON.parse(event.data);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=sse.js.map
|
package/dist/sse.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../sse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAYH;;;;;GAKG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,SAAS,CAAC,IAAc;IAC7C,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI;QAAE,OAAO;IAE/B,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IAClC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,yEAAyE;IACzE,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