Skip to content

Commit

Permalink
Add minimal spec for extra_params injection, fix formData injection a…
Browse files Browse the repository at this point in the history
…ppending params defined in base input
  • Loading branch information
Gregory Z. Szucs committed Sep 18, 2024
1 parent faa2f46 commit 4df2454
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 26 deletions.
7 changes: 4 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

const config = {
// preset: 'ts-jest',
// preset: 'ts-jest',
testEnvironment: 'node',
verbose: true,
transform: {
Expand All @@ -18,11 +18,12 @@ const config = {
// modulePathIgnorePatterns: ['/node_modules/'],
clearMocks: true,
collectCoverage: false,
// coverageDirectory: 'coverage',
// coverageReporters: ['html'],
// coverageDirectory: 'coverage',
// coverageReporters: ['html'],
moduleNameMapper: {
'^(.+)\\.js$': '$1',
},
setupFilesAfterEnv: ['./jest.setup.js'],
};

export default config;
2 changes: 2 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// jest.setup.js
require('jest-fetch-mock').enableMocks();
126 changes: 126 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 @@ -17,6 +17,7 @@
"test-e2e-clean": "jest --clearCache && jest --showConfig && jest --verbose --config ./jest.config.js",
"test-e2e": "jest --verbose --config ./jest.config.js",
"test-e2e-watch": "jest --verbose --config ./jest.config.js --watch",
"test-watch": "jest --verbose --config ./jest.config.js --watch",
"prepublishOnly": "tsc",
"postversion": "git push origin"
},
Expand All @@ -30,6 +31,7 @@
"babel-jest": "^29.7.0",
"eslint": "^8.48.0",
"jest": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"prettier": "^3.0.3",
"ts-jest": "^29.2.4",
"typescript": "^5.2.2"
Expand Down
15 changes: 11 additions & 4 deletions src/request-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export class RequestSender {
parameters: input.parameters ?? {},
});

// console.log('url', url, {
// protocol: this.clientState.protocol,
// domain: this.clientState.domain,
// apiVersion: this.clientState.apiVersion,
// path: input.path,
// parameters: input.parameters ?? {},
// });
let parsedBody;
if (input.body !== undefined && input.headers?.['Content-Type'] === 'application/json')
parsedBody = JSON.stringify(input.body);
Expand Down Expand Up @@ -76,10 +83,10 @@ export class RequestSender {
let formattedParameters: string;
if (Object.keys(parameters).length === 0) formattedParameters = '';
else {
/**
* @todo Check if we need to something about + signs ?
* cf. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#preserving_plus_signs
*/
/**
* @todo Check if we need to something about + signs ?
* cf. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#preserving_plus_signs
*/
formattedParameters = '?' + new URLSearchParams(parameters).toString();
}

Expand Down
1 change: 1 addition & 0 deletions src/resources/account.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class AccountResource {
const { limit, cursor } = input;

const parameters: Record<string, string> = { ...options?.extra_params };
// console.log('getAll', options, parameters);
if (limit) parameters.limit = String(limit);
if (cursor) parameters.cursor = cursor;

Expand Down
11 changes: 7 additions & 4 deletions src/resources/email.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,6 @@ export class EmailResource {
input;
const formDataBody = new FormData();

if (options?.extra_params) {
Object.entries(options.extra_params).forEach(([k, v]) => formDataBody.append(k, v));
}

formDataBody.append('body', body);
formDataBody.append('account_id', account_id);
if (draft_id) formDataBody.append('draft_id', draft_id);
Expand Down Expand Up @@ -321,6 +317,13 @@ export class EmailResource {
formDataBody.append('reply_to', reply_to);
}

if (options?.extra_params) {
Object.entries(options.extra_params).forEach(([k, v]) => {
if (!formDataBody.has(k)) {
formDataBody.append(k, v);
}
});
}
return await this.client.request.send({
path: ['emails'],
method: 'POST',
Expand Down
Loading

0 comments on commit 4df2454

Please sign in to comment.