Skip to content

Commit 905c8ef

Browse files
[RK] Resolve default transforms bug, don't override defaults in axios (#58)
1 parent 29a927b commit 905c8ef

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ api.listen(3000, () => {
3636
- [Express.js Usage](#usage-with-express)
3737
- [KOA Usage](#usage-with-koa)
3838
- [Examples](#code-examples)
39+
- [Upgrading From Alpha](#upgrading-from-alpha)
3940

4041
## Overview
4142

@@ -370,7 +371,6 @@ api.mount('GetImages').at('/images').withOption('transformRequest', (request, he
370371
371372
GraphQL Rest Router supports robust logging of incoming requests and errors. On instantiation, a logger of your choice can be injected with configurable log levels. The logger object must implement [ILogger](https://github.com/Econify/graphql-rest-router/blob/29cc328f23b8dd579a6f4af242266460e95e7d69/src/types.ts#L101-L107), and log levels must be one of the following [ILogLevels](https://github.com/Econify/graphql-rest-router/blob/f83881d30bdb329a306ebb94fdf577fb065f2e6e/src/types.ts#L107-L113).
372373
373-
374374
```js
375375
import GraphQLRestRouter, { LogLevels } from 'graphql-rest-router';
376376

@@ -525,7 +525,8 @@ As of the time of this writing, a KOA extension for GraphQL Rest Router is not a
525525
526526
See the [example client](/example-consuming-client) in this repo for code examples.
527527
528-
## Upgrading from alpha
528+
## Upgrading from Alpha
529+
529530
There is one breaking change with the release of `1.0.0-beta.0`: Transform response callbacks now receive parsed data as opposed to the stringified version. Therefore, any callback passed in this way must no longer parse prior to processing.
530531
531532
Chained route methods such as `disableCache()` or `transformResponse()` have been deprecated. Please use `withOption()` or `withOptions()` instead. Support for chained route methods will be removed in a future version.
@@ -542,4 +543,3 @@ api.mount('GetUserById').at('/users/:id').withOptions({
542543
transformResponse: cb,
543544
});
544545
```
545-

example-consuming-client/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Route.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ export default class Route implements IMountableItem {
124124
private schema!: DocumentNode;
125125
private logger!: Logger;
126126

127-
private transformRequestFn = axios.defaults.transformRequest as AxiosTransformer[];
128-
private transformResponseFn = axios.defaults.transformResponse as AxiosTransformer[];
127+
private transformRequestFn: AxiosTransformer[] = [];
128+
private transformResponseFn: AxiosTransformer[] = [];
129129

130130
private staticVariables: Record<string, unknown> = {};
131131
private defaultVariables: Record<string, unknown> = {};
@@ -138,6 +138,21 @@ export default class Route implements IMountableItem {
138138
this.configureRoute(configuration);
139139
}
140140

141+
private setDefaultTransforms() {
142+
const defaultTransformResponse = Array.isArray(axios.defaults.transformResponse)
143+
&& axios.defaults.transformResponse[0];
144+
const defaultTransformRequest = Array.isArray(axios.defaults.transformRequest)
145+
&& axios.defaults.transformRequest[0];
146+
147+
if (defaultTransformResponse) {
148+
this.transformResponseFn.push(defaultTransformResponse);
149+
}
150+
151+
if (defaultTransformRequest) {
152+
this.transformRequestFn.push(defaultTransformRequest);
153+
}
154+
}
155+
141156
private configureRoute(configuration: IConstructorRouteOptions) {
142157
const {
143158
schema,
@@ -151,6 +166,7 @@ export default class Route implements IMountableItem {
151166
throw new Error('A valid schema is required to initialize a Route');
152167
}
153168

169+
this.setDefaultTransforms();
154170
this.schema = typeof schema === 'string' ? parse(schema) : schema;
155171
this.axios = axios;
156172
this.logger = new Logger();

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"compilerOptions": {
3-
"lib": ["es2020"],
3+
"lib": ["es2019"],
44
"module": "commonjs",
5-
"target": "es2020",
5+
"target": "es2019",
66

77
"outDir": "build/src/",
88
"rootDir": "src/",

0 commit comments

Comments
 (0)