Skip to content

Commit

Permalink
Use lodash's cloneDeep to clone parsed body (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-Y-X authored Oct 17, 2023
1 parent dc17a01 commit f6cf377
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-ants-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/datasource-rest': patch
---

Use lodash's `cloneDeep` to clone parsed body instead of `JSON.parse(JSON.stringify(...))`
1 change: 1 addition & 0 deletions cspell-dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ careted
changesets
cimg
circleci
clonedeep
codesandbox
concat
datasource
Expand Down
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@changesets/changelog-github": "0.4.8",
"@changesets/cli": "2.26.2",
"@types/jest": "29.5.5",
"@types/lodash.clonedeep": "4.5.7",
"@types/lodash.isplainobject": "4.0.7",
"@types/node": "16.18.58",
"@typescript-eslint/eslint-plugin": "6.7.5",
Expand All @@ -59,6 +60,7 @@
"@apollo/utils.withrequired": "^3.0.0",
"@types/http-cache-semantics": "^4.0.1",
"http-cache-semantics": "^4.1.1",
"lodash.clonedeep": "^4.5.0",
"lodash.isplainobject": "^4.0.6",
"node-fetch": "^2.6.7"
},
Expand Down
12 changes: 5 additions & 7 deletions src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import type {
FetcherResponse,
} from '@apollo/utils.fetcher';
import type { KeyValueCache } from '@apollo/utils.keyvaluecache';
import type { Logger } from '@apollo/utils.logger';
import type { WithRequired } from '@apollo/utils.withrequired';
import { GraphQLError } from 'graphql';
import type { Options as HttpCacheSemanticsOptions } from 'http-cache-semantics';
import cloneDeep from 'lodash.clonedeep';
import isPlainObject from 'lodash.isplainobject';
import { HTTPCache } from './HTTPCache';
import type { Logger } from '@apollo/utils.logger';
import type { Options as HttpCacheSemanticsOptions } from 'http-cache-semantics';

export type ValueOrPromise<T> = T | Promise<T>;

Expand Down Expand Up @@ -326,11 +327,8 @@ export abstract class RESTDataSource<CO extends CacheOptions = CacheOptions> {
}

protected cloneParsedBody<TResult>(parsedBody: TResult) {
if (typeof parsedBody === 'string') {
return parsedBody;
} else {
return JSON.parse(JSON.stringify(parsedBody));
}
// consider using `structuredClone()` when we drop support for Node 16
return cloneDeep(parsedBody);
}

protected shouldJSONSerializeBody(body: RequestWithBody['body']): boolean {
Expand Down

0 comments on commit f6cf377

Please sign in to comment.