-
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.
- Loading branch information
Showing
9 changed files
with
155 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/types': patch | ||
'@graphql-mesh/plugin-serialize-headers': patch | ||
--- | ||
|
||
New Serialize Headers plugin |
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,45 @@ | ||
{ | ||
"name": "@graphql-mesh/plugin-serialize-headers", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "Urigo/graphql-mesh", | ||
"directory": "packages/plugins/serialize-headers" | ||
}, | ||
"license": "MIT", | ||
"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/types": "^0.91.13", | ||
"@graphql-mesh/utils": "^0.43.21", | ||
"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,23 @@ | ||
import { MeshPlugin, YamlConfig } from '@graphql-mesh/types'; | ||
|
||
export default function useMeshSerializeHeaders( | ||
options: YamlConfig.SerializeHeadersConfig, | ||
): MeshPlugin<any> { | ||
const map: Record<string, string> = {}; | ||
for (const headerName of options.names) { | ||
map[headerName.toLowerCase()] = headerName; | ||
} | ||
function headersSerializer(headers: Headers) { | ||
const headersObj: Record<string, string> = {}; | ||
for (const [key, value] of headers) { | ||
const finalKey = map[key] ?? key; | ||
headersObj[finalKey] = value; | ||
} | ||
return headersObj; | ||
} | ||
return { | ||
onFetch({ options }) { | ||
(options as any).headersSerializer = headersSerializer; | ||
}, | ||
}; | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/plugins/serialize-headers/test/serialize-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,47 @@ | ||
/* eslint-disable import/no-nodejs-modules */ | ||
|
||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import http from 'http'; | ||
import useMeshSerializeHeaders from '@graphql-mesh/plugin-serialize-headers'; | ||
import { wrapFetchWithPlugins } from '@graphql-mesh/runtime'; | ||
import { fetch } from '@whatwg-node/fetch'; | ||
|
||
describe('serialize-headers', () => { | ||
const server = http.createServer((req, res) => { | ||
res.end('ok'); | ||
}); | ||
beforeEach(done => { | ||
server.listen(0, done); | ||
}); | ||
afterEach(done => { | ||
server.close(done); | ||
}); | ||
it('works', async () => { | ||
jest.spyOn(http, 'request'); | ||
const fetchFn = wrapFetchWithPlugins([ | ||
{ | ||
onFetch({ setFetchFn }) { | ||
setFetchFn(fetch); | ||
}, | ||
}, | ||
useMeshSerializeHeaders({ | ||
names: ['x-FOO'], | ||
}), | ||
]); | ||
const fullUrl = `http://localhost:${(server.address() as any).port}/somePath`; | ||
const response = await fetchFn(fullUrl, { | ||
headers: { | ||
'x-foo': 'bar', | ||
}, | ||
}); | ||
await response.text(); | ||
expect(http.request).toHaveBeenCalledWith( | ||
fullUrl, | ||
expect.objectContaining({ | ||
headers: { | ||
'x-FOO': 'bar', | ||
}, | ||
}), | ||
); | ||
}); | ||
}); |
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,7 @@ | ||
extend type Plugin { | ||
serializeHeaders: SerializeHeadersConfig | ||
} | ||
|
||
type SerializeHeadersConfig @md { | ||
names: [String!]! | ||
} |
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
2 changes: 2 additions & 0 deletions
2
website/src/generated-markdown/SerializeHeadersConfig.generated.md
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,2 @@ | ||
|
||
* `names` (type: `Array of String`, required) |
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