asma-core-helpers 0.21.2 → 0.22.0

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.
@@ -0,0 +1,8 @@
1
+ import { type Client } from '@urql/core';
2
+ export interface CreateTadaServerClientOptions {
3
+ url: string;
4
+ jwt: string;
5
+ fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
6
+ }
7
+ export declare function createTadaServerClient({ url, jwt, fetch }: CreateTadaServerClientOptions): Client;
8
+ //# sourceMappingURL=createTadaServerClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createTadaServerClient.d.ts","sourceRoot":"","sources":["../../src/helpers/createTadaServerClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,KAAK,MAAM,EAAE,MAAM,YAAY,CAAA;AAErE,MAAM,WAAW,6BAA6B;IAC1C,GAAG,EAAE,MAAM,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC9E;AAED,wBAAgB,sBAAsB,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,6BAA6B,GAAG,MAAM,CAiBjG"}
@@ -0,0 +1,19 @@
1
+ import { createClient, fetchExchange } from '@urql/core';
2
+ export function createTadaServerClient({ url, jwt, fetch }) {
3
+ if (!jwt) {
4
+ throw new TypeError('A request JWT is required.');
5
+ }
6
+ return createClient({
7
+ url,
8
+ fetch,
9
+ fetchOptions: {
10
+ headers: {
11
+ Authorization: `Bearer ${jwt}`,
12
+ },
13
+ },
14
+ exchanges: [fetchExchange],
15
+ preferGetMethod: false,
16
+ requestPolicy: 'network-only',
17
+ });
18
+ }
19
+ //# sourceMappingURL=createTadaServerClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createTadaServerClient.js","sourceRoot":"","sources":["../../src/helpers/createTadaServerClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAe,MAAM,YAAY,CAAA;AAQrE,MAAM,UAAU,sBAAsB,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAiC;IACrF,IAAI,CAAC,GAAG,EAAE,CAAC;QACP,MAAM,IAAI,SAAS,CAAC,4BAA4B,CAAC,CAAA;IACrD,CAAC;IAED,OAAO,YAAY,CAAC;QAChB,GAAG;QACH,KAAK;QACL,YAAY,EAAE;YACV,OAAO,EAAE;gBACL,aAAa,EAAE,UAAU,GAAG,EAAE;aACjC;SACJ;QACD,SAAS,EAAE,CAAC,aAAa,CAAC;QAC1B,eAAe,EAAE,KAAK;QACtB,aAAa,EAAE,cAAc;KAChC,CAAC,CAAA;AACN,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asma-core-helpers",
3
- "version": "0.21.2",
3
+ "version": "0.22.0",
4
4
  "description": "Core helper utilities for ASMA applications",
5
5
  "files": [
6
6
  "lib/**/*",
@@ -15,6 +15,10 @@
15
15
  "types": "./lib/helpers/urql.d.ts",
16
16
  "default": "./lib/helpers/urql.js"
17
17
  },
18
+ "./urql-server": {
19
+ "types": "./lib/helpers/createTadaServerClient.d.ts",
20
+ "default": "./lib/helpers/createTadaServerClient.js"
21
+ },
18
22
  "./collaboration": {
19
23
  "types": "./lib/collaboration/index.d.ts",
20
24
  "default": "./lib/collaboration/index.js"
@@ -23,7 +27,8 @@
23
27
  "scripts": {
24
28
  "build": "pnpm ts:check",
25
29
  "ts:check": "tsc",
26
- "test": "node --import tsx --test \"src/**/*.test.ts\""
30
+ "test": "node --import tsx --test \"src/**/*.test.ts\"",
31
+ "test:package": "pnpm build && node --import tsx --test \"tests/*.test.ts\""
27
32
  },
28
33
  "repository": {
29
34
  "type": "git",
@@ -0,0 +1,23 @@
1
+ import assert from 'node:assert/strict'
2
+ import { test } from 'node:test'
3
+
4
+ import { parse } from '@0no-co/graphql.web'
5
+
6
+ import { createTadaServerClient } from './createTadaServerClient.js'
7
+
8
+ test('forwards the exact request JWT to the upstream GraphQL service', async () => {
9
+ let authorizationHeader: string | null = null
10
+ const client = createTadaServerClient({
11
+ url: 'https://wrapper.example/v1/graphql',
12
+ jwt: 'request-jwt',
13
+ fetch: async (_input, init) => {
14
+ authorizationHeader = new Headers(init?.headers).get('authorization')
15
+ return Response.json({ data: { __typename: 'Query' } })
16
+ },
17
+ })
18
+
19
+ const result = await client.query(parse('query ServerClientTest { __typename }'), {}).toPromise()
20
+
21
+ assert.equal(result.error, undefined)
22
+ assert.equal(authorizationHeader, 'Bearer request-jwt')
23
+ })
@@ -0,0 +1,26 @@
1
+ import { createClient, fetchExchange, type Client } from '@urql/core'
2
+
3
+ export interface CreateTadaServerClientOptions {
4
+ url: string
5
+ jwt: string
6
+ fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>
7
+ }
8
+
9
+ export function createTadaServerClient({ url, jwt, fetch }: CreateTadaServerClientOptions): Client {
10
+ if (!jwt) {
11
+ throw new TypeError('A request JWT is required.')
12
+ }
13
+
14
+ return createClient({
15
+ url,
16
+ fetch,
17
+ fetchOptions: {
18
+ headers: {
19
+ Authorization: `Bearer ${jwt}`,
20
+ },
21
+ },
22
+ exchanges: [fetchExchange],
23
+ preferGetMethod: false,
24
+ requestPolicy: 'network-only',
25
+ })
26
+ }