@nestbridge/runtime 1.0.0 → 1.1.1
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/README.md +30 -9
- package/build/nestbridge.runtime.d.ts +73 -50
- package/build/nestbridge.runtime.js +142 -152
- package/build/nestbridge.runtime.js.map +1 -1
- package/package.json +6 -7
package/README.md
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
[](https://github.com/MDReal32/nestbridge/actions/workflows/ci.yml)
|
|
4
4
|
[](https://www.npmjs.com/package/@nestbridge/runtime)
|
|
5
5
|
|
|
6
|
-
The browser-side
|
|
7
|
-
modules call into
|
|
8
|
-
|
|
9
|
-
of
|
|
6
|
+
The browser-side client that NestBridge's generated controller and resolver
|
|
7
|
+
modules call into: a dependency-free `fetch()` wrapper for REST, and a
|
|
8
|
+
[`graphql-request`](https://github.com/jasonkuhrt/graphql-request)-based
|
|
9
|
+
client for GraphQL. No knowledge of NestJS or Vite either way — see the
|
|
10
|
+
[repository README](../../README.md) for how this fits into the rest of
|
|
11
|
+
NestBridge.
|
|
10
12
|
|
|
11
13
|
## Install
|
|
12
14
|
|
|
@@ -17,7 +19,7 @@ yarn add @nestbridge/runtime
|
|
|
17
19
|
## Configure once
|
|
18
20
|
|
|
19
21
|
```ts
|
|
20
|
-
import { configureNestBridge } from '
|
|
22
|
+
import { configureNestBridge } from 'nestbridge';
|
|
21
23
|
|
|
22
24
|
configureNestBridge({
|
|
23
25
|
baseURL: '/api',
|
|
@@ -34,6 +36,7 @@ than merging, so pass everything you need in one call.
|
|
|
34
36
|
```ts
|
|
35
37
|
interface NestBridgeConfig {
|
|
36
38
|
baseURL?: string;
|
|
39
|
+
graphqlEndpoint?: string; // defaults to `${baseURL}/graphql`
|
|
37
40
|
headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
38
41
|
fetch?: typeof globalThis.fetch;
|
|
39
42
|
}
|
|
@@ -52,14 +55,32 @@ class NestBridgeError<T = unknown> extends Error {
|
|
|
52
55
|
}
|
|
53
56
|
```
|
|
54
57
|
|
|
58
|
+
## `graphqlRequest()`
|
|
59
|
+
|
|
60
|
+
Generated resolver modules call `graphqlRequest()` directly; you won't
|
|
61
|
+
normally call it yourself either. It sends the given document/variables to
|
|
62
|
+
`graphqlEndpoint` via `graphql-request`'s `GraphQLClient`. Failed requests
|
|
63
|
+
reject with a `NestBridgeGraphqlError`:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
class NestBridgeGraphqlError extends Error {
|
|
67
|
+
readonly status: number;
|
|
68
|
+
readonly errors: GraphQLError[] | undefined;
|
|
69
|
+
readonly data: Record<string, unknown> | undefined;
|
|
70
|
+
readonly request: unknown;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
55
74
|
## Types
|
|
56
75
|
|
|
57
76
|
- `NestBridgeConfig` — the shape passed to `configureNestBridge`.
|
|
58
|
-
- `NestBridgeRequest` — the shape of a single generated request.
|
|
77
|
+
- `NestBridgeRequest` — the shape of a single generated REST request.
|
|
78
|
+
- `NestBridgeGraphqlRequest` — the shape of a single generated GraphQL
|
|
79
|
+
request (`document` + optional `variables`).
|
|
59
80
|
- `RemoteMethod<T>` / `RemoteResult<T>` — the type-level helpers that project
|
|
60
|
-
a real controller method's parameters and return type onto its
|
|
61
|
-
client method. Useful outside code generation too — a manual SDK
|
|
62
|
-
mock, a test double.
|
|
81
|
+
a real controller or resolver method's parameters and return type onto its
|
|
82
|
+
generated client method. Useful outside code generation too — a manual SDK
|
|
83
|
+
wrapper, a mock, a test double.
|
|
63
84
|
|
|
64
85
|
## License
|
|
65
86
|
|
|
@@ -1,50 +1,73 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
export declare
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
export declare
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
//#region src/config/nestbridge-config.d.ts
|
|
3
|
+
interface NestBridgeConfig {
|
|
4
|
+
baseURL?: string;
|
|
5
|
+
graphqlEndpoint?: string;
|
|
6
|
+
headers?: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
7
|
+
fetch?: typeof globalThis.fetch;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/config/configure-nestbridge.d.ts
|
|
11
|
+
export declare const configureNestBridge: (config: NestBridgeConfig) => void;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/graphql-adapter/nestbridge-graphql-request.d.ts
|
|
14
|
+
interface NestBridgeGraphqlRequest {
|
|
15
|
+
document: string;
|
|
16
|
+
variables?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/graphql-adapter/graphql-request.d.ts
|
|
20
|
+
export declare const graphqlRequest: <T>(nestBridgeGraphqlRequest: NestBridgeGraphqlRequest) => Promise<T>;
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/graphql-adapter/extract-graphql-error-message.d.ts
|
|
23
|
+
interface GraphqlErrorResponse {
|
|
24
|
+
status: number;
|
|
25
|
+
headers: Headers;
|
|
26
|
+
body: string;
|
|
27
|
+
errors?: Array<{
|
|
28
|
+
message: string;
|
|
29
|
+
}>;
|
|
30
|
+
data?: unknown;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/graphql-adapter/nestbridge-graphql-error.d.ts
|
|
34
|
+
export declare class NestBridgeGraphqlError extends Error {
|
|
35
|
+
readonly status: number;
|
|
36
|
+
readonly errors: GraphqlErrorResponse["errors"];
|
|
37
|
+
readonly data: GraphqlErrorResponse["data"];
|
|
38
|
+
constructor(response: GraphqlErrorResponse, endpoint: string);
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/http-adapter/nestbridge-error.d.ts
|
|
42
|
+
export declare class NestBridgeError<T = unknown> extends Error {
|
|
43
|
+
readonly status: number;
|
|
44
|
+
readonly body: T;
|
|
45
|
+
readonly response: Response;
|
|
46
|
+
constructor(message: string, status: number, body: T, response: Response);
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/http-adapter/nestbridge-request.d.ts
|
|
50
|
+
interface NestBridgeRequest {
|
|
51
|
+
method: string;
|
|
52
|
+
path: string;
|
|
53
|
+
query?: Record<string, unknown>;
|
|
54
|
+
body?: unknown;
|
|
55
|
+
headers?: Record<string, string | undefined>;
|
|
56
|
+
responseType?: "json" | "blob";
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/http-adapter/request.d.ts
|
|
60
|
+
export declare const request: <T>(nestBridgeRequest: NestBridgeRequest) => Promise<T>;
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/remote-method.d.ts
|
|
63
|
+
type RemoteResult<T> = T extends ((...args: never[]) => infer Result) ? Promise<Awaited<Result>> : never;
|
|
64
|
+
type UnwrapObservableResult<Result> = Result extends Observable<infer Value> ? Value : Result extends Promise<Observable<infer Value>> ? Value : never;
|
|
65
|
+
type RemoteObservableResult<T> = T extends ((...args: never[]) => infer Result) ? Promise<UnwrapObservableResult<Result>> : never;
|
|
66
|
+
type RemoteStreamResult<T> = T extends ((...args: never[]) => unknown) ? Promise<Blob> : never;
|
|
67
|
+
type RemoteMethod<T> = T extends ((...args: infer Args) => unknown) ? (...args: Args) => RemoteResult<T> : never;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/version.d.ts
|
|
70
|
+
export declare const VERSION: string;
|
|
71
|
+
//#endregion
|
|
72
|
+
export type { NestBridgeConfig, NestBridgeGraphqlRequest, NestBridgeRequest, RemoteMethod, RemoteObservableResult, RemoteResult, RemoteStreamResult };
|
|
73
|
+
//# sourceMappingURL=nestbridge.runtime.d.ts.map
|
|
@@ -1,162 +1,152 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
currentConfig = config;
|
|
1
|
+
//#region src/config/configure-nestbridge.ts
|
|
2
|
+
var currentConfig = {};
|
|
3
|
+
var configureNestBridge = (config) => {
|
|
4
|
+
currentConfig = config;
|
|
6
5
|
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (value !== void 0) {
|
|
17
|
-
merged[key] = value;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return merged;
|
|
6
|
+
var getNestBridgeConfig = () => currentConfig;
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/http-adapter/parse-response-body.ts
|
|
9
|
+
var parseResponseBody = async (response) => {
|
|
10
|
+
if (response.status === 204 || response.status === 205) return;
|
|
11
|
+
const text = await response.text();
|
|
12
|
+
if (text.length === 0) return;
|
|
13
|
+
if (!(response.headers.get("content-type") ?? "").includes("application/json")) return text;
|
|
14
|
+
return JSON.parse(text);
|
|
21
15
|
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
super(clientError.message);
|
|
30
|
-
this.name = "NestBridgeGraphqlError";
|
|
31
|
-
this.status = clientError.response.status;
|
|
32
|
-
this.errors = clientError.response.errors;
|
|
33
|
-
this.data = clientError.response.data;
|
|
34
|
-
this.request = clientError.request;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const isAbsoluteUrl = (value) => /^[a-z][a-z\d+.-]*:\/\//i.test(value);
|
|
39
|
-
const resolveGraphqlEndpoint = (config) => {
|
|
40
|
-
const endpoint = config.graphqlEndpoint ?? `${config.baseURL ?? ""}/graphql`;
|
|
41
|
-
if (isAbsoluteUrl(endpoint) || typeof globalThis.location === "undefined") {
|
|
42
|
-
return endpoint;
|
|
43
|
-
}
|
|
44
|
-
return new URL(endpoint, globalThis.location.origin).toString();
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/http-adapter/resolve-request-headers.ts
|
|
18
|
+
var resolveRequestHeaders = async (config, requestHeaders, hasBody) => {
|
|
19
|
+
const merged = { ...typeof config.headers === "function" ? await config.headers() : config.headers ?? {} };
|
|
20
|
+
if (hasBody) merged["content-type"] = "application/json";
|
|
21
|
+
for (const [key, value] of Object.entries(requestHeaders ?? {})) if (value !== void 0) merged[key] = value;
|
|
22
|
+
return merged;
|
|
45
23
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const client = new GraphQLClient(endpoint, { fetch: config.fetch, headers });
|
|
52
|
-
try {
|
|
53
|
-
return await client.request(
|
|
54
|
-
nestBridgeGraphqlRequest.document,
|
|
55
|
-
nestBridgeGraphqlRequest.variables
|
|
56
|
-
);
|
|
57
|
-
} catch (error) {
|
|
58
|
-
if (error instanceof ClientError) {
|
|
59
|
-
throw new NestBridgeGraphqlError(error);
|
|
60
|
-
}
|
|
61
|
-
throw error;
|
|
62
|
-
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/graphql-adapter/extract-graphql-error-message.ts
|
|
26
|
+
var extractGraphqlErrorMessage = (response) => {
|
|
27
|
+
if (response.errors !== void 0 && response.errors.length > 0) return response.errors.map((error) => error.message).join(", ");
|
|
28
|
+
if (typeof response.body === "string" && response.body.length > 0) return response.body;
|
|
63
29
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const searchParams = new URLSearchParams();
|
|
80
|
-
for (const [key, value] of Object.entries(query)) {
|
|
81
|
-
if (value === void 0 || value === null) {
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
if (Array.isArray(value)) {
|
|
85
|
-
for (const item of value) {
|
|
86
|
-
searchParams.append(key, String(item));
|
|
87
|
-
}
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
searchParams.append(key, String(value));
|
|
91
|
-
}
|
|
92
|
-
return searchParams.toString();
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/graphql-adapter/nestbridge-graphql-error.ts
|
|
32
|
+
var NestBridgeGraphqlError = class extends Error {
|
|
33
|
+
status;
|
|
34
|
+
errors;
|
|
35
|
+
data;
|
|
36
|
+
constructor(response, endpoint) {
|
|
37
|
+
const backendMessage = extractGraphqlErrorMessage(response);
|
|
38
|
+
const message = `NestBridge GraphQL request to ${endpoint} failed with status ${response.status}.${backendMessage === void 0 ? "" : ` ${backendMessage}`}`;
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = "NestBridgeGraphqlError";
|
|
41
|
+
this.status = response.status;
|
|
42
|
+
this.errors = response.errors;
|
|
43
|
+
this.data = response.data;
|
|
44
|
+
}
|
|
93
45
|
};
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/graphql-adapter/resolve-graphql-endpoint.ts
|
|
48
|
+
var isAbsoluteUrl = (value) => /^[a-z][a-z\d+.-]*:\/\//i.test(value);
|
|
49
|
+
var resolveGraphqlEndpoint = (config) => {
|
|
50
|
+
const endpoint = config.graphqlEndpoint ?? `${config.baseURL ?? ""}/graphql`;
|
|
51
|
+
if (isAbsoluteUrl(endpoint) || typeof globalThis.location === "undefined") return endpoint;
|
|
52
|
+
return new URL(endpoint, globalThis.location.origin).toString();
|
|
101
53
|
};
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/graphql-adapter/graphql-request.ts
|
|
56
|
+
var isGraphqlResponseBody = (body) => typeof body === "object" && body !== null;
|
|
57
|
+
var graphqlRequest = async (nestBridgeGraphqlRequest) => {
|
|
58
|
+
const config = getNestBridgeConfig();
|
|
59
|
+
const fetchImplementation = config.fetch ?? globalThis.fetch;
|
|
60
|
+
const endpoint = resolveGraphqlEndpoint(config);
|
|
61
|
+
const response = await fetchImplementation(endpoint, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: await resolveRequestHeaders(config, void 0, true),
|
|
64
|
+
body: JSON.stringify({
|
|
65
|
+
query: nestBridgeGraphqlRequest.document,
|
|
66
|
+
variables: nestBridgeGraphqlRequest.variables
|
|
67
|
+
})
|
|
68
|
+
});
|
|
69
|
+
const responseBody = await parseResponseBody(response);
|
|
70
|
+
const body = isGraphqlResponseBody(responseBody) ? responseBody : {};
|
|
71
|
+
if (!response.ok || body.errors !== void 0 && body.errors.length > 0) throw new NestBridgeGraphqlError({
|
|
72
|
+
status: response.status,
|
|
73
|
+
headers: response.headers,
|
|
74
|
+
body: typeof responseBody === "string" ? responseBody : "",
|
|
75
|
+
errors: body.errors,
|
|
76
|
+
data: body.data
|
|
77
|
+
}, endpoint);
|
|
78
|
+
return body.data;
|
|
120
79
|
};
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return JSON.parse(text);
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/http-adapter/nestbridge-error.ts
|
|
82
|
+
var NestBridgeError = class extends Error {
|
|
83
|
+
status;
|
|
84
|
+
body;
|
|
85
|
+
response;
|
|
86
|
+
constructor(message, status, body, response) {
|
|
87
|
+
super(message);
|
|
88
|
+
this.name = "NestBridgeError";
|
|
89
|
+
this.status = status;
|
|
90
|
+
this.body = body;
|
|
91
|
+
this.response = response;
|
|
92
|
+
}
|
|
135
93
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (!response.ok) {
|
|
150
|
-
const backendMessage = extractErrorMessage(responseBody);
|
|
151
|
-
const message = `NestBridge request to ${url} failed with status ${response.status}.${backendMessage === void 0 ? "" : ` ${backendMessage}`}`;
|
|
152
|
-
throw new NestBridgeError(message, response.status, responseBody, response);
|
|
153
|
-
}
|
|
154
|
-
return responseBody;
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/http-adapter/build-request-url.ts
|
|
96
|
+
var serializeQuery = (query) => {
|
|
97
|
+
const searchParams = new URLSearchParams();
|
|
98
|
+
for (const [key, value] of Object.entries(query)) {
|
|
99
|
+
if (value === void 0 || value === null) continue;
|
|
100
|
+
if (Array.isArray(value)) {
|
|
101
|
+
for (const item of value) searchParams.append(key, String(item));
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
searchParams.append(key, String(value));
|
|
105
|
+
}
|
|
106
|
+
return searchParams.toString();
|
|
155
107
|
};
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
108
|
+
var buildRequestUrl = (baseURL, path, query) => {
|
|
109
|
+
const url = `${baseURL ?? ""}${path}`;
|
|
110
|
+
if (query === void 0) return url;
|
|
111
|
+
const queryString = serializeQuery(query);
|
|
112
|
+
return queryString.length > 0 ? `${url}?${queryString}` : url;
|
|
113
|
+
};
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/http-adapter/extract-error-message.ts
|
|
116
|
+
var extractErrorMessage = (body) => {
|
|
117
|
+
if (typeof body === "string") return body.length > 0 ? body : void 0;
|
|
118
|
+
if (typeof body === "object" && body !== null && "message" in body) {
|
|
119
|
+
const { message } = body;
|
|
120
|
+
if (typeof message === "string") return message;
|
|
121
|
+
if (Array.isArray(message)) return message.join(", ");
|
|
122
|
+
}
|
|
123
|
+
if (typeof body === "object" && body !== null) return JSON.stringify(body);
|
|
124
|
+
};
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/http-adapter/request.ts
|
|
127
|
+
var request = async (nestBridgeRequest) => {
|
|
128
|
+
const config = getNestBridgeConfig();
|
|
129
|
+
const fetchImplementation = config.fetch ?? globalThis.fetch;
|
|
130
|
+
const hasBody = nestBridgeRequest.body !== void 0;
|
|
131
|
+
const url = buildRequestUrl(config.baseURL, nestBridgeRequest.path, nestBridgeRequest.query);
|
|
132
|
+
const headers = await resolveRequestHeaders(config, nestBridgeRequest.headers, hasBody);
|
|
133
|
+
const response = await fetchImplementation(url, {
|
|
134
|
+
method: nestBridgeRequest.method,
|
|
135
|
+
headers,
|
|
136
|
+
body: hasBody ? JSON.stringify(nestBridgeRequest.body) : void 0
|
|
137
|
+
});
|
|
138
|
+
if (!response.ok) {
|
|
139
|
+
const responseBody = await parseResponseBody(response);
|
|
140
|
+
const backendMessage = extractErrorMessage(responseBody);
|
|
141
|
+
throw new NestBridgeError(`NestBridge request to ${url} failed with status ${response.status}.${backendMessage === void 0 ? "" : ` ${backendMessage}`}`, response.status, responseBody, response);
|
|
142
|
+
}
|
|
143
|
+
if (nestBridgeRequest.responseType === "blob") return await response.blob();
|
|
144
|
+
return await parseResponseBody(response);
|
|
145
|
+
};
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/version.ts
|
|
148
|
+
var VERSION = "1.1.1";
|
|
149
|
+
//#endregion
|
|
161
150
|
export { NestBridgeError, NestBridgeGraphqlError, VERSION, configureNestBridge, graphqlRequest, request };
|
|
162
|
-
|
|
151
|
+
|
|
152
|
+
//# sourceMappingURL=nestbridge.runtime.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nestbridge.runtime.js","sources":["../src/config/configure-nestbridge.ts","../src/http-adapter/resolve-request-headers.ts","../src/graphql-adapter/nestbridge-graphql-error.ts","../src/graphql-adapter/resolve-graphql-endpoint.ts","../src/graphql-adapter/graphql-request.ts","../src/http-adapter/nestbridge-error.ts","../src/http-adapter/build-request-url.ts","../src/http-adapter/extract-error-message.ts","../src/http-adapter/parse-response-body.ts","../src/http-adapter/request.ts","../src/version.ts"],"sourcesContent":["import type { NestBridgeConfig } from './nestbridge-config';\n\nlet currentConfig: NestBridgeConfig = {};\n\nexport const configureNestBridge = (config: NestBridgeConfig) => {\n currentConfig = config;\n};\n\nexport const getNestBridgeConfig = () => currentConfig;\n","import type { NestBridgeConfig } from '../config/nestbridge-config';\n\nexport const resolveRequestHeaders = async (\n config: NestBridgeConfig,\n requestHeaders: Record<string, string | undefined> | undefined,\n hasBody: boolean,\n) => {\n const globalHeaders =\n typeof config.headers === 'function' ? await config.headers() : (config.headers ?? {});\n const merged: Record<string, string> = { ...globalHeaders };\n\n if (hasBody) {\n merged['content-type'] = 'application/json';\n }\n\n for (const [key, value] of Object.entries(requestHeaders ?? {})) {\n if (value !== undefined) {\n merged[key] = value;\n }\n }\n\n return merged;\n};\n","import type { ClientError } from 'graphql-request';\n\nexport class NestBridgeGraphqlError extends Error {\n readonly status: number;\n readonly errors: ClientError['response']['errors'];\n readonly data: ClientError['response']['data'];\n readonly request: ClientError['request'];\n\n constructor(clientError: ClientError) {\n super(clientError.message);\n this.name = 'NestBridgeGraphqlError';\n this.status = clientError.response.status;\n this.errors = clientError.response.errors;\n this.data = clientError.response.data;\n this.request = clientError.request;\n }\n}\n","import type { NestBridgeConfig } from '../config';\n\nconst isAbsoluteUrl = (value: string) => /^[a-z][a-z\\d+.-]*:\\/\\//i.test(value);\n\nexport const resolveGraphqlEndpoint = (config: NestBridgeConfig) => {\n const endpoint = config.graphqlEndpoint ?? `${config.baseURL ?? ''}/graphql`;\n\n if (isAbsoluteUrl(endpoint) || typeof globalThis.location === 'undefined') {\n return endpoint;\n }\n\n return new URL(endpoint, globalThis.location.origin).toString();\n};\n","import { ClientError, GraphQLClient } from 'graphql-request';\n\nimport { getNestBridgeConfig } from '../config';\nimport { resolveRequestHeaders } from '../http-adapter/resolve-request-headers';\nimport { NestBridgeGraphqlError } from './nestbridge-graphql-error';\nimport type { NestBridgeGraphqlRequest } from './nestbridge-graphql-request';\nimport { resolveGraphqlEndpoint } from './resolve-graphql-endpoint';\n\nexport const graphqlRequest = async <T>(nestBridgeGraphqlRequest: NestBridgeGraphqlRequest) => {\n const config = getNestBridgeConfig();\n const endpoint = resolveGraphqlEndpoint(config);\n const headers = await resolveRequestHeaders(config, undefined, false);\n const client = new GraphQLClient(endpoint, { fetch: config.fetch, headers });\n\n try {\n return await client.request<T>(\n nestBridgeGraphqlRequest.document,\n nestBridgeGraphqlRequest.variables,\n );\n } catch (error) {\n if (error instanceof ClientError) {\n throw new NestBridgeGraphqlError(error);\n }\n\n throw error;\n }\n};\n","export class NestBridgeError<T = unknown> extends Error {\n readonly status: number;\n readonly body: T;\n readonly response: Response;\n\n constructor(message: string, status: number, body: T, response: Response) {\n super(message);\n this.name = 'NestBridgeError';\n this.status = status;\n this.body = body;\n this.response = response;\n }\n}\n","const serializeQuery = (query: Record<string, unknown>) => {\n const searchParams = new URLSearchParams();\n\n for (const [key, value] of Object.entries(query)) {\n if (value === undefined || value === null) {\n continue;\n }\n\n if (Array.isArray(value)) {\n for (const item of value) {\n searchParams.append(key, String(item));\n }\n continue;\n }\n\n searchParams.append(key, String(value));\n }\n\n return searchParams.toString();\n};\n\nexport const buildRequestUrl = (\n baseURL: string | undefined,\n path: string,\n query: Record<string, unknown> | undefined,\n) => {\n const url = `${baseURL ?? ''}${path}`;\n\n if (query === undefined) {\n return url;\n }\n\n const queryString = serializeQuery(query);\n\n return queryString.length > 0 ? `${url}?${queryString}` : url;\n};\n","export const extractErrorMessage = (body: unknown): string | undefined => {\n if (typeof body === 'string') {\n return body.length > 0 ? body : undefined;\n }\n\n if (typeof body === 'object' && body !== null && 'message' in body) {\n const { message } = body as { message: unknown };\n\n if (typeof message === 'string') {\n return message;\n }\n\n if (Array.isArray(message)) {\n return message.join(', ');\n }\n }\n\n if (typeof body === 'object' && body !== null) {\n return JSON.stringify(body);\n }\n\n return undefined;\n};\n","export const parseResponseBody = async (response: Response): Promise<unknown> => {\n if (response.status === 204 || response.status === 205) {\n return undefined;\n }\n\n const text = await response.text();\n\n if (text.length === 0) {\n return undefined;\n }\n\n const contentType = response.headers.get('content-type') ?? '';\n\n if (!contentType.includes('application/json')) {\n return text;\n }\n\n return JSON.parse(text);\n};\n","import { getNestBridgeConfig } from '../config';\nimport { buildRequestUrl } from './build-request-url';\nimport { extractErrorMessage } from './extract-error-message';\nimport { NestBridgeError } from './nestbridge-error';\nimport type { NestBridgeRequest } from './nestbridge-request';\nimport { parseResponseBody } from './parse-response-body';\nimport { resolveRequestHeaders } from './resolve-request-headers';\n\nexport const request = async <T>(nestBridgeRequest: NestBridgeRequest) => {\n const config = getNestBridgeConfig();\n const fetchImplementation = config.fetch ?? globalThis.fetch;\n const hasBody = nestBridgeRequest.body !== undefined;\n const url = buildRequestUrl(config.baseURL, nestBridgeRequest.path, nestBridgeRequest.query);\n const headers = await resolveRequestHeaders(config, nestBridgeRequest.headers, hasBody);\n\n const response = await fetchImplementation(url, {\n method: nestBridgeRequest.method,\n headers,\n body: hasBody ? JSON.stringify(nestBridgeRequest.body) : undefined,\n });\n\n const responseBody = await parseResponseBody(response);\n\n if (!response.ok) {\n const backendMessage = extractErrorMessage(responseBody);\n const message = `NestBridge request to ${url} failed with status ${response.status}.${\n backendMessage === undefined ? '' : ` ${backendMessage}`\n }`;\n\n throw new NestBridgeError(message, response.status, responseBody, response);\n }\n\n return responseBody as T;\n};\n","import { version } from '../package.json';\n\nexport const VERSION: string = version;\n"],"names":[],"mappings":";;AAEA,IAAI,gBAAkC,EAAC;AAEhC,MAAM,mBAAA,GAAsB,CAAC,MAAA,KAA6B;AAC/D,EAAA,aAAA,GAAgB,MAAA;AAClB;AAEO,MAAM,sBAAsB,MAAM,aAAA;;ACNlC,MAAM,qBAAA,GAAwB,OACnC,MAAA,EACA,cAAA,EACA,OAAA,KACG;AACH,EAAA,MAAM,aAAA,GACJ,OAAO,MAAA,CAAO,OAAA,KAAY,UAAA,GAAa,MAAM,MAAA,CAAO,OAAA,EAAQ,GAAK,MAAA,CAAO,OAAA,IAAW,EAAC;AACtF,EAAA,MAAM,MAAA,GAAiC,EAAE,GAAG,aAAA,EAAc;AAE1D,EAAA,IAAI,OAAA,EAAS;AACX,IAAA,MAAA,CAAO,cAAc,CAAA,GAAI,kBAAA;AAAA,EAC3B;AAEA,EAAA,KAAA,MAAW,CAAC,KAAK,KAAK,CAAA,IAAK,OAAO,OAAA,CAAQ,cAAA,IAAkB,EAAE,CAAA,EAAG;AAC/D,IAAA,IAAI,UAAU,MAAA,EAAW;AACvB,MAAA,MAAA,CAAO,GAAG,CAAA,GAAI,KAAA;AAAA,IAChB;AAAA,EACF;AAEA,EAAA,OAAO,MAAA;AACT,CAAA;;ACpBO,MAAM,+BAA+B,KAAA,CAAM;AAAA,EACvC,MAAA;AAAA,EACA,MA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iB,oBAAoB,YAAY,CAAA;AACvD,IAAA,MAAM,OAAA,GAAU,CAAA,sBAAA,EAAyB,GAAG,CAAA,oBAAA,EAAuB,QAAA,CAAS,MAAM,CAAA,CAAA,EAChF,cAAA,KAAmB,MAAA,GAAY,EAAA,GAAK,CAAA,CAAA,EAAI,cAAc,CAAA,CACxD,CAAA,CAAA;AAEA,IAAA,MAAM,IAAI,eAAA,CAAgB,OAAA,EAAS,QAAA,CAAS,MAAA,EAAQ,cAAc,QAAQ,CAAA;AAAA,EAC5E;AAEA,EAAA,OAAO,YAAA;AACT;;;;AC/BO,MAAM,OAAA,GAAkB;;"}
|
|
1
|
+
{"version":3,"file":"nestbridge.runtime.js","names":[],"sources":["../src/config/configure-nestbridge.ts","../src/http-adapter/parse-response-body.ts","../src/http-adapter/resolve-request-headers.ts","../src/graphql-adapter/extract-graphql-error-message.ts","../src/graphql-adapter/nestbridge-graphql-error.ts","../src/graphql-adapter/resolve-graphql-endpoint.ts","../src/graphql-adapter/graphql-request.ts","../src/http-adapter/nestbridge-error.ts","../src/http-adapter/build-request-url.ts","../src/http-adapter/extract-error-message.ts","../src/http-adapter/request.ts","../package.json","../src/version.ts"],"sourcesContent":["import type { NestBridgeConfig } from './nestbridge-config';\n\nlet currentConfig: NestBridgeConfig = {};\n\nexport const configureNestBridge = (config: NestBridgeConfig): void => {\n currentConfig = config;\n};\n\nexport const getNestBridgeConfig = (): NestBridgeConfig => currentConfig;\n","export const parseResponseBody = async (response: Response): Promise<unknown> => {\n if (response.status === 204 || response.status === 205) {\n return undefined;\n }\n\n const text = await response.text();\n\n if (text.length === 0) {\n return undefined;\n }\n\n const contentType = response.headers.get('content-type') ?? '';\n\n if (!contentType.includes('application/json')) {\n return text;\n }\n\n return JSON.parse(text);\n};\n","import type { NestBridgeConfig } from '../config/nestbridge-config';\n\nexport const resolveRequestHeaders = async (\n config: NestBridgeConfig,\n requestHeaders: Record<string, string | undefined> | undefined,\n hasBody: boolean,\n): Promise<Record<string, string>> => {\n const globalHeaders =\n typeof config.headers === 'function' ? await config.headers() : (config.headers ?? {});\n const merged: Record<string, string> = { ...globalHeaders };\n\n if (hasBody) {\n merged['content-type'] = 'application/json';\n }\n\n for (const [key, value] of Object.entries(requestHeaders ?? {})) {\n if (value !== undefined) {\n merged[key] = value;\n }\n }\n\n return merged;\n};\n","export interface GraphqlErrorResponse {\n status: number;\n headers: Headers;\n body: string;\n errors?: Array<{ message: string }>;\n data?: unknown;\n}\n\nexport const extractGraphqlErrorMessage = (response: GraphqlErrorResponse): string | undefined => {\n if (response.errors !== undefined && response.errors.length > 0) {\n return response.errors.map((error) => error.message).join(', ');\n }\n\n if (typeof response.body === 'string' && response.body.length > 0) {\n return response.body;\n }\n\n return undefined;\n};\n","import {\n extractGraphqlErrorMessage,\n type GraphqlErrorResponse,\n} from './extract-graphql-error-message';\n\nexport class NestBridgeGraphqlError extends Error {\n readonly status: number;\n readonly errors: GraphqlErrorResponse['errors'];\n readonly data: GraphqlErrorResponse['data'];\n\n constructor(response: GraphqlErrorResponse, endpoint: string) {\n const backendMessage = extractGraphqlErrorMessage(response);\n const message = `NestBridge GraphQL request to ${endpoint} failed with status ${\n response.status\n }.${backendMessage === undefined ? '' : ` ${backendMessage}`}`;\n\n super(message);\n this.name = 'NestBridgeGraphqlError';\n this.status = response.status;\n this.errors = response.errors;\n this.data = response.data;\n }\n}\n","import type { NestBridgeConfig } from '../config';\n\nconst isAbsoluteUrl = (value: string) => /^[a-z][a-z\\d+.-]*:\\/\\//i.test(value);\n\nexport const resolveGraphqlEndpoint = (config: NestBridgeConfig): string => {\n const endpoint = config.graphqlEndpoint ?? `${config.baseURL ?? ''}/graphql`;\n\n if (isAbsoluteUrl(endpoint) || typeof globalThis.location === 'undefined') {\n return endpoint;\n }\n\n return new URL(endpoint, globalThis.location.origin).toString();\n};\n","import { getNestBridgeConfig } from '../config';\nimport { parseResponseBody } from '../http-adapter/parse-response-body';\nimport { resolveRequestHeaders } from '../http-adapter/resolve-request-headers';\nimport { NestBridgeGraphqlError } from './nestbridge-graphql-error';\nimport type { NestBridgeGraphqlRequest } from './nestbridge-graphql-request';\nimport { resolveGraphqlEndpoint } from './resolve-graphql-endpoint';\n\ninterface GraphqlResponseBody<T> {\n data?: T;\n errors?: Array<{ message: string }>;\n}\n\nconst isGraphqlResponseBody = <T>(body: unknown): body is GraphqlResponseBody<T> =>\n typeof body === 'object' && body !== null;\n\nexport const graphqlRequest = async <T>(\n nestBridgeGraphqlRequest: NestBridgeGraphqlRequest,\n): Promise<T> => {\n const config = getNestBridgeConfig();\n const fetchImplementation = config.fetch ?? globalThis.fetch;\n const endpoint = resolveGraphqlEndpoint(config);\n const headers = await resolveRequestHeaders(config, undefined, true);\n\n const response = await fetchImplementation(endpoint, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n query: nestBridgeGraphqlRequest.document,\n variables: nestBridgeGraphqlRequest.variables,\n }),\n });\n\n const responseBody = await parseResponseBody(response);\n const body = isGraphqlResponseBody<T>(responseBody) ? responseBody : {};\n\n if (!response.ok || (body.errors !== undefined && body.errors.length > 0)) {\n throw new NestBridgeGraphqlError(\n {\n status: response.status,\n headers: response.headers,\n body: typeof responseBody === 'string' ? responseBody : '',\n errors: body.errors,\n data: body.data,\n },\n endpoint,\n );\n }\n\n return body.data as T;\n};\n","export class NestBridgeError<T = unknown> extends Error {\n readonly status: number;\n readonly body: T;\n readonly response: Response;\n\n constructor(message: string, status: number, body: T, response: Response) {\n super(message);\n this.name = 'NestBridgeError';\n this.status = status;\n this.body = body;\n this.response = response;\n }\n}\n","const serializeQuery = (query: Record<string, unknown>) => {\n const searchParams = new URLSearchParams();\n\n for (const [key, value] of Object.entries(query)) {\n if (value === undefined || value === null) {\n continue;\n }\n\n if (Array.isArray(value)) {\n for (const item of value) {\n searchParams.append(key, String(item));\n }\n continue;\n }\n\n searchParams.append(key, String(value));\n }\n\n return searchParams.toString();\n};\n\nexport const buildRequestUrl = (\n baseURL: string | undefined,\n path: string,\n query: Record<string, unknown> | undefined,\n): string => {\n const url = `${baseURL ?? ''}${path}`;\n\n if (query === undefined) {\n return url;\n }\n\n const queryString = serializeQuery(query);\n\n return queryString.length > 0 ? `${url}?${queryString}` : url;\n};\n","export const extractErrorMessage = (body: unknown): string | undefined => {\n if (typeof body === 'string') {\n return body.length > 0 ? body : undefined;\n }\n\n if (typeof body === 'object' && body !== null && 'message' in body) {\n const { message } = body as { message: unknown };\n\n if (typeof message === 'string') {\n return message;\n }\n\n if (Array.isArray(message)) {\n return message.join(', ');\n }\n }\n\n if (typeof body === 'object' && body !== null) {\n return JSON.stringify(body);\n }\n\n return undefined;\n};\n","import { getNestBridgeConfig } from '../config';\nimport { buildRequestUrl } from './build-request-url';\nimport { extractErrorMessage } from './extract-error-message';\nimport { NestBridgeError } from './nestbridge-error';\nimport type { NestBridgeRequest } from './nestbridge-request';\nimport { parseResponseBody } from './parse-response-body';\nimport { resolveRequestHeaders } from './resolve-request-headers';\n\nexport const request = async <T>(nestBridgeRequest: NestBridgeRequest): Promise<T> => {\n const config = getNestBridgeConfig();\n const fetchImplementation = config.fetch ?? globalThis.fetch;\n const hasBody = nestBridgeRequest.body !== undefined;\n const url = buildRequestUrl(config.baseURL, nestBridgeRequest.path, nestBridgeRequest.query);\n const headers = await resolveRequestHeaders(config, nestBridgeRequest.headers, hasBody);\n\n const response = await fetchImplementation(url, {\n method: nestBridgeRequest.method,\n headers,\n body: hasBody ? JSON.stringify(nestBridgeRequest.body) : undefined,\n });\n\n if (!response.ok) {\n const responseBody = await parseResponseBody(response);\n const backendMessage = extractErrorMessage(responseBody);\n const message = `NestBridge request to ${url} failed with status ${response.status}.${\n backendMessage === undefined ? '' : ` ${backendMessage}`\n }`;\n\n throw new NestBridgeError(message, response.status, responseBody, response);\n }\n\n if (nestBridgeRequest.responseType === 'blob') {\n return (await response.blob()) as T;\n }\n\n return (await parseResponseBody(response)) as T;\n};\n","{\n \"name\": \"@nestbridge/runtime\",\n \"version\": \"1.1.1\",\n \"type\": \"module\",\n \"license\": \"MIT\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/MDReal32/nestbridge.git\",\n \"directory\": \"packages/runtime\"\n },\n \"main\": \"./build/nestbridge.runtime.js\",\n \"types\": \"./build/nestbridge.runtime.d.ts\",\n \"exports\": {\n \".\": {\n \"types\": \"./build/nestbridge.runtime.d.ts\",\n \"import\": \"./build/nestbridge.runtime.js\"\n }\n },\n \"files\": [\n \"build\"\n ],\n \"scripts\": {\n \"build\": \"vite build\",\n \"test\": \"vitest run\",\n \"typecheck\": \"tsc -p tsconfig.json --noEmit\",\n \"lint\": \"biome check .\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^26.4.1\",\n \"typescript\": \"^7.0.2\",\n \"vite\": \"^8.2.2\",\n \"vite-plugin-dts\": \"^5.1.0\",\n \"vitest\": \"^5.0.0\"\n },\n \"dependencies\": {\n \"rxjs\": \"^7.8.2\"\n }\n}\n","import { version } from '../package.json';\n\nexport const VERSION: string = version;\n"],"mappings":";AAEA,IAAI,gBAAkC,CAAC;AAEvC,IAAa,uBAAuB,WAAmC;CACrE,gBAAgB;AAClB;AAEA,IAAa,4BAA8C;;;ACR3D,IAAa,oBAAoB,OAAO,aAAyC;CAC/E,IAAI,SAAS,WAAW,OAAO,SAAS,WAAW,KACjD;CAGF,MAAM,OAAO,MAAM,SAAS,KAAK;CAEjC,IAAI,KAAK,WAAW,GAClB;CAKF,IAAI,EAFgB,SAAS,QAAQ,IAAI,cAAc,KAAK,GAAA,CAE3C,SAAS,kBAAkB,GAC1C,OAAO;CAGT,OAAO,KAAK,MAAM,IAAI;AACxB;;;AChBA,IAAa,wBAAwB,OACnC,QACA,gBACA,YACoC;CAGpC,MAAM,SAAiC,EAAE,GADvC,OAAO,OAAO,YAAY,aAAa,MAAM,OAAO,QAAQ,IAAK,OAAO,WAAW,CAAC,EAC5B;CAE1D,IAAI,SACF,OAAO,kBAAkB;CAG3B,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QA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IT",
|
|
6
6
|
"repository": {
|
|
@@ -26,14 +26,13 @@
|
|
|
26
26
|
"lint": "biome check ."
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@types/node": "^
|
|
30
|
-
"typescript": "^
|
|
31
|
-
"vite": "^
|
|
32
|
-
"vite-plugin-dts": "^
|
|
29
|
+
"@types/node": "^26.4.1",
|
|
30
|
+
"typescript": "^7.0.2",
|
|
31
|
+
"vite": "^8.2.2",
|
|
32
|
+
"vite-plugin-dts": "^5.1.0",
|
|
33
33
|
"vitest": "^5.0.0"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"
|
|
37
|
-
"graphql-request": "^7.4.0"
|
|
36
|
+
"rxjs": "^7.8.2"
|
|
38
37
|
}
|
|
39
38
|
}
|