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

fix(superagent-wrapper): revert avoid mutation by sketchy codecs #913

Merged
merged 1 commit into from
Oct 4, 2024
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: 2 additions & 4 deletions packages/superagent-wrapper/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ const patchRequest = <
});
}
return pipe(
// deep copy to prevent modification of the inputs by sketchy codecs
route.response[status].decode(structuredClone(res.body)),
route.response[status].decode(res.body),
E.map((body) =>
decodedResponse<Route>({
status,
Expand Down Expand Up @@ -195,8 +194,7 @@ export const requestForRoute =
route: Route,
): BoundRequestFactory<Req, Route> =>
(params: h.RequestType<Route>): PatchedRequest<Req, Route> => {
// deep copy to prevent modification of the inputs by sketchy codecs
const reqProps = route.request.encode(structuredClone(params));
const reqProps = route.request.encode(params);

let path = route.path;
for (const key in reqProps.params) {
Expand Down
67 changes: 0 additions & 67 deletions packages/superagent-wrapper/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,6 @@ const PostTestRoute = h.httpRoute({
},
});

const PostOptionalTestRoute = h.httpRoute({
path: '/test/optional/{id}',
method: 'POST',
request: h.httpRequest({
query: {
foo: t.string,
},
params: {
id: NumberFromString,
},
body: {
bar: t.number,
optional: h.optionalized({
baz: t.boolean,
qux: h.optional(t.string),
}),
},
}),
response: {
200: t.type({
id: t.number,
foo: t.string,
bar: t.number,
baz: t.boolean,
}),
401: t.type({
message: t.string,
}),
},
});

const HeaderGetTestRoute = h.httpRoute({
path: '/getHeader',
method: 'GET',
Expand All @@ -91,9 +60,6 @@ const TestRoutes = h.apiSpec({
'api.v1.test': {
post: PostTestRoute,
},
'api.v1.test.optional': {
post: PostOptionalTestRoute,
},
'api.v1.getheader': {
get: HeaderGetTestRoute,
},
Expand Down Expand Up @@ -139,23 +105,6 @@ const createTestServer = (port: number) => {
}
});

testApp.post('/test/optional/:id', (req, res) => {
const filteredReq = {
query: req.query,
params: req.params,
headers: req.headers,
body: req.body,
};
const params = E.getOrElseW((err) => {
throw new Error(JSON.stringify(err));
})(PostTestRoute.request.decode(filteredReq));
const response = PostTestRoute.response[200].encode({
...params,
baz: true,
});
res.send(response);
});

testApp.get(HeaderGetTestRoute.path, (req, res) => {
res.send(
HeaderGetTestRoute.response[200].encode({
Expand Down Expand Up @@ -293,22 +242,6 @@ describe('decodeExpecting', () => {
'Could not decode response 200: [{"invalid":"response"}] due to error [Invalid value undefined supplied to : { id: number, foo: string, bar: number, baz: boolean }/id: number\nInvalid value undefined supplied to : { id: number, foo: string, bar: number, baz: boolean }/foo: string\nInvalid value undefined supplied to : { id: number, foo: string, bar: number, baz: boolean }/bar: number\nInvalid value undefined supplied to : { id: number, foo: string, bar: number, baz: boolean }/baz: boolean]',
);
});

it('does not modify inputs by dropping keys with undefined values', async () => {
const body = {
id: 1337,
foo: 'test',
bar: 42,
optional: { baz: true, qux: undefined },
};
await apiClient['api.v1.test.optional'].post(body).decodeExpecting(200);
assert.deepEqual(body, {
id: 1337,
foo: 'test',
bar: 42,
optional: { baz: true, qux: undefined },
});
});
});

describe('superagent', async () => {
Expand Down