Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize Headers plugin #5333

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/hot-queens-beg.md
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
45 changes: 45 additions & 0 deletions packages/plugins/serialize-headers/package.json
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"
}
}
23 changes: 23 additions & 0 deletions packages/plugins/serialize-headers/src/index.ts
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 packages/plugins/serialize-headers/test/serialize-headers.test.ts
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',
},
}),
);
});
});
7 changes: 7 additions & 0 deletions packages/plugins/serialize-headers/yaml-config.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extend type Plugin {
serializeHeaders: SerializeHeadersConfig
}

type SerializeHeadersConfig @md {
names: [String!]!
}
18 changes: 18 additions & 0 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@
"responseCache": {
"$ref": "#/definitions/ResponseCacheConfig"
},
"serializeHeaders": {
"$ref": "#/definitions/SerializeHeadersConfig"
},
"snapshot": {
"$ref": "#/definitions/SnapshotPluginConfig",
"description": "Configuration for Snapshot extension"
Expand Down Expand Up @@ -2674,6 +2677,21 @@
},
"required": ["coordinate", "ttl"]
},
"SerializeHeadersConfig": {
"additionalProperties": false,
"type": "object",
"title": "SerializeHeadersConfig",
"properties": {
"names": {
"type": "array",
"items": {
"type": "string"
},
"additionalItems": false
}
},
"required": ["names"]
},
"SnapshotPluginConfig": {
"additionalProperties": false,
"type": "object",
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,7 @@ export interface Plugin {
prometheus?: PrometheusConfig;
rateLimit?: RateLimitPluginConfig;
responseCache?: ResponseCacheConfig;
serializeHeaders?: SerializeHeadersConfig;
snapshot?: SnapshotPluginConfig;
statsd?: StatsdPlugin;
[k: string]: any;
Expand Down Expand Up @@ -2125,6 +2126,9 @@ export interface ResponseCacheTTLConfig {
coordinate: string;
ttl: number;
}
export interface SerializeHeadersConfig {
names: string[];
}
/**
* Configuration for Snapshot extension
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

* `names` (type: `Array of String`, required)
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6019,9 +6019,9 @@
tslib "^2.3.1"

"@whatwg-node/node-fetch@^0.3.3":
version "0.3.4"
resolved "https://registry.yarnpkg.com/@whatwg-node/node-fetch/-/node-fetch-0.3.4.tgz#4beb88579c53ebd870e28d0f2f0376191b9fd6c3"
integrity sha512-gP1MN6DiHVbhkLWH1eCELhE2ZtLRxb+HRKu4eYze1Tijxz0uT1T2kk3lseZp94txzxCfbxGFU0jsWkxNdH3EXA==
version "0.3.5"
resolved "https://registry.yarnpkg.com/@whatwg-node/node-fetch/-/node-fetch-0.3.5.tgz#a0e76ef72c33b0c81728421ce6d7d97257541ecb"
integrity sha512-96crxTZn6L+xFefEKkeAZrGmZ7WUXDYUzAfBf1VtrdS5YozLnFbj9/CNZ8S2LdRS2iL3pMSCvE1xD1wiIAXkAA==
dependencies:
"@whatwg-node/events" "^0.0.2"
busboy "^1.6.0"
Expand Down