Skip to content

Commit

Permalink
feat: use safe-json-stringify for http pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiOvchinnikov authored Mar 4, 2020
1 parent 36bdd8d commit e2517db
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@qiwi/substrate": "^1.18.11",
"@types/lodash.once": "^4.1.6",
"lodash.once": "^4.1.1",
"safe-json-stringify": "^1.2.0",
"tslib": "^1.11.1"
},
"devDependencies": {
Expand All @@ -63,9 +64,9 @@
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.0.4",
"@semantic-release/npm": "^7.0.3",
"semantic-release": "^17.0.4",
"@types/jest": "^25.1.3",
"@types/jest-json-schema": "^2.1.1",
"@types/safe-json-stringify": "^1.1.0",
"@typescript-eslint/eslint-plugin": "^2.21.0",
"@typescript-eslint/parser": "^2.21.0",
"coveralls": "^3.0.9",
Expand All @@ -79,6 +80,7 @@
"jest": "^25.1.0",
"replace-in-file": "^5.0.2",
"rimraf": "^3.0.2",
"semantic-release": "^17.0.4",
"terser": "^4.6.4",
"ts-jest": "^25.2.1",
"typedoc": "^0.16.11",
Expand Down
12 changes: 10 additions & 2 deletions src/main/ts/pipes/http.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import safeJsonStringify from 'safe-json-stringify'
import { HttpMethod, IPromise } from '@qiwi/substrate'
import { IPipe, IPipeOutput, ITransmittable } from '../interfaces'

Expand All @@ -20,10 +21,17 @@ const getPlainHeaders = (headers: IHttpHeaders): Record<string, string> =>
export const createHttpPipe = ({ url, method, headers }: IHttpPipeOpts): IPipe => ({
type,
execute ({ data }: ITransmittable): IPromise<IPipeOutput> {
const defaultHeaders = {
'Content-Type': 'application/json'
}

return fetch(url, {
method,
headers: headers && getPlainHeaders(headers),
body: data
headers: {
...defaultHeaders,
...(headers && getPlainHeaders(headers))
},
body: data && safeJsonStringify(data)
})
.then(async (res) => {
if (!res.ok) {
Expand Down
5 changes: 3 additions & 2 deletions src/test/ts/pipes/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ describe('httpPipe', () => {
b: () => 'bar'
}
})
const transittable: ITransmittable = { data: null, meta: { history: [] } }
const transittable: ITransmittable = { data: { message: 'bar' }, meta: { history: [] } }

await httpPipe.execute(transittable, noop)
expect(spy).toHaveBeenCalledWith('https://reqres.in/api/users/1', {
method: HttpMethod.GET,
body: null,
body: '{"message":"bar"}',
headers: {
'Content-Type': 'application/json',
a: 'foo',
b: 'bar'
}
Expand Down
46 changes: 12 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,11 @@
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==

"@types/safe-json-stringify@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@types/safe-json-stringify/-/safe-json-stringify-1.1.0.tgz#4cd786442d7abc037f8e9026b22e3b401005c287"
integrity sha512-iIQqHp8fqDgxTlWor4DrTrKGVmjDeGDodQBipQkPSlRU1QeKIytv37U4aFN9N65VJcFJx67+zOnpbTNQzqHTOg==

"@types/stack-utils@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
Expand Down Expand Up @@ -1949,7 +1954,7 @@ debug@^3.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -3481,7 +3486,7 @@ import-modules@^2.0.0:
resolved "https://registry.yarnpkg.com/import-modules/-/import-modules-2.0.0.tgz#9c1e13b4e7a15682f70a6e3fa29534e4540cfc5d"
integrity sha512-iczM/v9drffdNnABOKwj0f9G3cFDon99VcG1mxeBsdqnbd+vnQ5c2uAiCHNQITqFTOPaEvwg3VjoWCur0uHLEw==

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -4694,11 +4699,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -4707,33 +4707,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -4799,11 +4777,6 @@ lodash.once@^4.1.1:
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.snakecase@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz#39d714a35357147837aefd64b5dcbb16becd8f8d"
Expand Down Expand Up @@ -6803,6 +6776,11 @@ safe-buffer@~5.1.0, safe-buffer@~5.1.1:
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==

safe-json-stringify@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/safe-json-stringify/-/safe-json-stringify-1.2.0.tgz#356e44bc98f1f93ce45df14bcd7c01cda86e0afd"
integrity sha512-gH8eh2nZudPQO6TytOvbxnuhYBOvDBBLW52tz5q6X58lJcd/tkmqFR+5Z9adS8aJtURSXWThWy/xJtJwixErvg==

safe-regex@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/safe-regex/-/safe-regex-1.1.0.tgz#40a3669f3b077d1e943d44629e157dd48023bf2e"
Expand Down

0 comments on commit e2517db

Please sign in to comment.