-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New plugin: useOperationHeaders (#6610)
* New plugin: useOperationHeaders * Tests * chore(dependencies): updated changesets for modified dependencies * Relax Lint * use setOptions * Runtime * Fix lockfile * chore(dependencies): updated changesets for modified dependencies --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
381126e
commit dbaf72c
Showing
6 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@graphql-mesh/plugin-operation-headers": patch | ||
"@graphql-mesh/types": patch | ||
--- | ||
|
||
New Plugin: Operation Headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "@graphql-mesh/plugin-operation-headers", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "ardatan/graphql-mesh", | ||
"directory": "packages/plugins/operation-headers" | ||
}, | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"exports": { | ||
".": { | ||
"require": { | ||
"types": "./dist/typings/index.d.cts", | ||
"default": "./dist/cjs/index.js" | ||
}, | ||
"import": { | ||
"types": "./dist/typings/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"default": { | ||
"types": "./dist/typings/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"typings": "dist/typings/index.d.ts", | ||
"peerDependencies": { | ||
"@graphql-mesh/serve-runtime": "^0.1.1", | ||
"@graphql-mesh/types": "^0.96.6", | ||
"@graphql-mesh/utils": "^0.96.6", | ||
"graphql": "*", | ||
"tslib": "^2.4.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"directory": "dist" | ||
}, | ||
"sideEffects": false, | ||
"typescript": { | ||
"definition": "dist/typings/index.d.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { MeshServeContext, MeshServePlugin } from '@graphql-mesh/serve-runtime'; | ||
import type { MeshFetchRequestInit } from '@graphql-mesh/types'; | ||
import { getHeadersObj, mapMaybePromise } from '@graphql-mesh/utils'; | ||
|
||
export interface OperationHeadersFactoryPayload { | ||
context: MeshServeContext; | ||
url: string; | ||
options: MeshFetchRequestInit; | ||
} | ||
|
||
export type OperationHeadersFactory = ( | ||
payload: OperationHeadersFactoryPayload, | ||
) => Promise<Record<string, string>> | Record<string, string>; | ||
|
||
export function useOperationHeaders(factoryFn: OperationHeadersFactory): MeshServePlugin { | ||
return { | ||
onFetch({ url, options, context, setOptions }) { | ||
const existingHeaders = getHeadersObj(options.headers || {}); | ||
const newHeaders$ = factoryFn({ | ||
url, | ||
options, | ||
context, | ||
}); | ||
return mapMaybePromise(newHeaders$, newHeaders => { | ||
setOptions({ | ||
...options, | ||
headers: { | ||
...existingHeaders, | ||
...newHeaders, | ||
}, | ||
}); | ||
}); | ||
}, | ||
}; | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/plugins/operation-headers/test/operation-headers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useOperationHeaders } from '@graphql-mesh/plugin-operation-headers'; | ||
import { wrapFetchWithPlugins } from '@graphql-mesh/runtime'; | ||
|
||
describe('Operation Headers', () => { | ||
it('works', async () => { | ||
const wrappedFetch = wrapFetchWithPlugins([ | ||
{ | ||
onFetch({ setFetchFn }) { | ||
setFetchFn(async (url, opts) => { | ||
const req = new Request(url, opts); | ||
if (req.headers.get('Authorization') !== 'Bearer test') { | ||
return new Response('Unauthorized', { status: 401 }); | ||
} | ||
return Response.json({ data: 'test' }); | ||
}); | ||
}, | ||
}, | ||
useOperationHeaders(({ context }) => ({ | ||
Authorization: `Bearer ${context.headers['x-auth-token']}`, | ||
})), | ||
]); | ||
const authorizedRes = await wrappedFetch( | ||
'http://localhost:3000', | ||
{}, | ||
{ | ||
headers: { | ||
'x-auth-token': 'test', | ||
}, | ||
}, | ||
); | ||
expect(authorizedRes.status).toBe(200); | ||
const authorizedResJson = await authorizedRes.json(); | ||
expect(authorizedResJson).toEqual({ data: 'test' }); | ||
|
||
const unauthorizedRes = await wrappedFetch( | ||
'http://localhost:3000', | ||
{}, | ||
{ | ||
headers: { | ||
'x-auth-token': 'wrong-token', | ||
}, | ||
}, | ||
); | ||
expect(unauthorizedRes.status).toBe(401); | ||
const unauthorizedResText = await unauthorizedRes.text(); | ||
expect(unauthorizedResText).toBe('Unauthorized'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters