AAE-30882 Install ofetch, add unit tests for existing implementation

This commit is contained in:
Wojciech Duda 2025-02-17 17:17:04 +01:00
parent e4feb68381
commit 8eaf2ab741
4 changed files with 154 additions and 52 deletions

@ -20,10 +20,9 @@ import { SuperagentHttpClient } from '../src/superagentHttpClient';
import { Response } from 'superagent';
describe('SuperagentHttpClient', () => {
describe('#buildRequest', () => {
const client = new SuperagentHttpClient();
describe('buildRequest', () => {
it('should create a request with response type blob', () => {
const client = new SuperagentHttpClient();
const queryParams = {};
const headerParams = {};
const formParams = {};
@ -68,8 +67,8 @@ describe('SuperagentHttpClient', () => {
});
});
describe('#deserialize', () => {
it('should the deserializer return an array of object when the response is an array', () => {
describe('deserialize', () => {
it('should deserialize to an array when the response body is an array', () => {
const data = {
body: [
{
@ -86,5 +85,76 @@ describe('SuperagentHttpClient', () => {
const isArray = Array.isArray(result);
assert.equal(isArray, true);
});
it('should deserialize to an object when the response body is an object', () => {
const data = {
body: {
id: '1',
name: 'test1'
}
} as Response;
const result = SuperagentHttpClient['deserialize'](data);
const isArray = Array.isArray(result);
assert.equal(isArray, false);
});
it('should return null when response is null', () => {
const result = SuperagentHttpClient['deserialize'](null);
assert.equal(result, null);
});
it('should fallback to text property when body is null', () => {
const data = {
text: '{"id": "1", "name": "test1"}',
header: {
'content-type': 'application/json'
}
} as any as Response;
const result = SuperagentHttpClient['deserialize'](data, 'blob');
assert.deepEqual(result, new Blob([data.text], { type: data.header['content-type'] }));
});
it('should convert to returnType when provided', () => {
class Dummy {
id: string;
name: string;
constructor(data: any) {
this.id = data.id;
this.name = data.name;
}
}
const data = {
body: {
id: '1',
name: 'test1'
}
} as Response;
const result = SuperagentHttpClient['deserialize'](data, Dummy);
assert.ok(result instanceof Dummy);
assert.equal(result.id, '1');
assert.equal(result.name, 'test1');
});
});
describe('setCsrfToken', () => {
it('should set CSRF token in headers and document.cookie', () => {
const client = new SuperagentHttpClient();
const fakeRequest: any = {
header: {},
set: function (key: string, value: string) {
this.header[key] = value;
return this;
},
withCredentials: function () {}
};
if (typeof document === 'undefined') {
(global as any).document = { cookie: '' };
}
client.setCsrfToken(fakeRequest);
assert.ok(fakeRequest.header['X-CSRF-TOKEN']);
assert.ok(document.cookie.indexOf('CSRF-TOKEN=') !== -1);
});
});
});

@ -7,6 +7,7 @@
"esModuleInterop": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"lib": ["ESNext", "dom"]
"lib": ["ESNext", "dom"],
"types": ["jest"]
}
}

120
package-lock.json generated

@ -41,6 +41,7 @@
"material-icons": "^1.13.12",
"minimatch": "^10.0.1",
"ng2-charts": "^4.1.1",
"ofetch": "^1.4.1",
"pdfjs-dist": "3.3.122",
"raphael": "2.3.0",
"rxjs": "7.8.1",
@ -5555,6 +5556,27 @@
"node": ">= 6"
}
},
"node_modules/@mapbox/node-pre-gyp/node_modules/node-fetch": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"license": "MIT",
"optional": true,
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/@mapbox/node-pre-gyp/node_modules/nopt": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz",
@ -5588,6 +5610,31 @@
"url": "https://github.com/sponsors/isaacs"
}
},
"node_modules/@mapbox/node-pre-gyp/node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"license": "MIT",
"optional": true
},
"node_modules/@mapbox/node-pre-gyp/node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"license": "BSD-2-Clause",
"optional": true
},
"node_modules/@mapbox/node-pre-gyp/node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"license": "MIT",
"optional": true,
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/@mat-datetimepicker/core": {
"version": "13.0.2",
"resolved": "https://registry.npmjs.org/@mat-datetimepicker/core/-/core-13.0.2.tgz",
@ -22551,6 +22598,12 @@
"node": ">=6"
}
},
"node_modules/destr": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/destr/-/destr-2.0.3.tgz",
"integrity": "sha512-2N3BOUU4gYMpTP24s5rF5iP7BDr7uNTCs4ozw3kf/eKfvWSIu93GEBi5m427YoyJoeOzQ5smuu4nNAPGb8idSQ==",
"license": "MIT"
},
"node_modules/destroy": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
@ -31651,51 +31704,11 @@
"license": "MIT",
"optional": true
},
"node_modules/node-fetch": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"license": "MIT",
"optional": true,
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/node-fetch/node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"license": "MIT",
"optional": true
},
"node_modules/node-fetch/node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"license": "BSD-2-Clause",
"optional": true
},
"node_modules/node-fetch/node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"license": "MIT",
"optional": true,
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
"node_modules/node-fetch-native": {
"version": "1.6.6",
"resolved": "https://registry.npmjs.org/node-fetch-native/-/node-fetch-native-1.6.6.tgz",
"integrity": "sha512-8Mc2HhqPdlIfedsuZoc3yioPuzp6b+L5jRCRY1QzuWZh2EGJVQrGppC6V6cF0bLdbW0+O2YpqCA25aF/1lvipQ==",
"license": "MIT"
},
"node_modules/node-forge": {
"version": "1.3.1",
@ -32607,6 +32620,17 @@
"integrity": "sha512-PX1wu0AmAdPqOL1mWhqmlOd8kOIZQwGZw6rh7uby9fTc5lhaOWFLX3I6R1hrF9k3zUY40e6igsLGkDXK92LJNg==",
"license": "MIT"
},
"node_modules/ofetch": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/ofetch/-/ofetch-1.4.1.tgz",
"integrity": "sha512-QZj2DfGplQAr2oj9KzceK9Hwz6Whxazmn85yYeVuS3u9XTMOGMRx0kO95MQ+vLsj/S/NwBDMMLU5hpxvI6Tklw==",
"license": "MIT",
"dependencies": {
"destr": "^2.0.3",
"node-fetch-native": "^1.6.4",
"ufo": "^1.5.4"
}
},
"node_modules/on-finished": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz",
@ -38918,6 +38942,12 @@
"node": "*"
}
},
"node_modules/ufo": {
"version": "1.5.4",
"resolved": "https://registry.npmjs.org/ufo/-/ufo-1.5.4.tgz",
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ==",
"license": "MIT"
},
"node_modules/unbox-primitive": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.1.0.tgz",

@ -61,6 +61,7 @@
"material-icons": "^1.13.12",
"minimatch": "^10.0.1",
"ng2-charts": "^4.1.1",
"ofetch": "^1.4.1",
"pdfjs-dist": "3.3.122",
"raphael": "2.3.0",
"rxjs": "7.8.1",
@ -98,7 +99,6 @@
"@nx/workspace": "17.3.1",
"@paperist/types-remark": "0.1.3",
"@playwright/test": "1.46.1",
"@valano/change-font-size": "^1.0.0",
"@schematics/angular": "17.1.4",
"@storybook/addon-essentials": "8.4.7",
"@storybook/angular": "^8.4.6",
@ -120,6 +120,7 @@
"@typescript-eslint/parser": "6.21.0",
"@typescript-eslint/typescript-estree": "7.1.1",
"@typescript-eslint/utils": "^8.8.1",
"@valano/change-font-size": "^1.0.0",
"ajv": "^8.12.0",
"commander": "12.0.0",
"css-loader": "^7.1.2",