chore: add fetch funtcion & add tests to helper module
This commit is contained in:
parent
e289aeea27
commit
7b5e820285
22 changed files with 315 additions and 17 deletions
1
packages/helper/dist/fetch.d.ts
vendored
Normal file
1
packages/helper/dist/fetch.d.ts
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export default function fetch(url: string, options?: RequestInit): Promise<Response>;
|
||||
8
packages/helper/dist/fetch.js
vendored
Normal file
8
packages/helper/dist/fetch.js
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const isomorphic_unfetch_1 = require("isomorphic-unfetch");
|
||||
function fetch(url, options = {}) {
|
||||
return (0, isomorphic_unfetch_1.default)(url, Object.assign({}, options));
|
||||
}
|
||||
exports.default = fetch;
|
||||
//# sourceMappingURL=fetch.js.map
|
||||
1
packages/helper/dist/fetch.js.map
vendored
Normal file
1
packages/helper/dist/fetch.js.map
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../fetch.ts"],"names":[],"mappings":";;AAAA,2DAAuC;AAEvC,SAAwB,KAAK,CAAC,GAAW,EAAE,UAAuB,EAAE;IAClE,OAAO,IAAA,4BAAM,EAAC,GAAG,oBAAO,OAAO,EAAG,CAAA;AACpC,CAAC;AAFD,wBAEC"}
|
||||
1
packages/helper/dist/fetch.test.d.ts
vendored
Normal file
1
packages/helper/dist/fetch.test.d.ts
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export {};
|
||||
33
packages/helper/dist/fetch.test.js
vendored
Normal file
33
packages/helper/dist/fetch.test.js
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fetch_1 = require("./fetch");
|
||||
const isomorphic_unfetch_1 = require("isomorphic-unfetch");
|
||||
jest.mock("isomorphic-unfetch", () => jest.fn(() => Promise.resolve(isomorphic_unfetch_1.default)));
|
||||
describe("fetch", () => {
|
||||
const url = "https://example.com";
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
it("should make a GET request with default options", async () => {
|
||||
const response = await (0, fetch_1.default)(url);
|
||||
expect(response.ok).toBe(true);
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.statusText).toBe("OK");
|
||||
expect(isomorphic_unfetch_1.default).toHaveBeenCalledWith(url, {});
|
||||
});
|
||||
it("should make a POST request with options", async () => {
|
||||
const options = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ data: "test" }),
|
||||
};
|
||||
const response = await (0, fetch_1.default)(url, options);
|
||||
expect(response.ok).toBe(true);
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.statusText).toBe("OK");
|
||||
expect(isomorphic_unfetch_1.default).toHaveBeenCalledWith(url, Object.assign(Object.assign({}, options), { body: JSON.stringify({ data: "test" }) }));
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=fetch.test.js.map
|
||||
1
packages/helper/dist/fetch.test.js.map
vendored
Normal file
1
packages/helper/dist/fetch.test.js.map
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"fetch.test.js","sourceRoot":"","sources":["../fetch.test.ts"],"names":[],"mappings":";;AAAA,mCAA2B;AAC3B,2DAAkD;AAElD,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,4BAAiB,CAAC,CAAC,CAAC,CAAA;AAExF,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE;IACrB,MAAM,GAAG,GAAG,qBAAqB,CAAA;IAEjC,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,aAAa,EAAE,CAAA;IACtB,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,gDAAgD,EAAE,KAAK,IAAI,EAAE;QAC9D,MA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.ts
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from "./fetch";
|
||||
18
packages/helper/dist/index.js
vendored
Normal file
18
packages/helper/dist/index.js
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__exportStar(require("./fetch"), exports);
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
packages/helper/dist/index.js.map
vendored
Normal file
1
packages/helper/dist/index.js.map
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB"}
|
||||
1
packages/helper/dist/tsconfig.tsbuildinfo
vendored
Normal file
1
packages/helper/dist/tsconfig.tsbuildinfo
vendored
Normal file
File diff suppressed because one or more lines are too long
11
packages/helper/fetch.test.ts
Normal file
11
packages/helper/fetch.test.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { fetch } from "./fetch"
|
||||
|
||||
describe("myFetch()", () => {
|
||||
it("should return data from the server", async () => {
|
||||
const response = await fetch("https://example.com")
|
||||
expect(response.ok).toBe(true)
|
||||
expect(response.status).toBe(200)
|
||||
const data = await response.text()
|
||||
expect(data).toMatch("Example Domain")
|
||||
})
|
||||
})
|
||||
5
packages/helper/fetch.ts
Normal file
5
packages/helper/fetch.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import _fetch from "isomorphic-unfetch"
|
||||
|
||||
export function fetch(url: string, options: RequestInit = {}): Promise<Response> {
|
||||
return _fetch(url, { ...options })
|
||||
}
|
||||
1
packages/helper/index.ts
Normal file
1
packages/helper/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export * from "./fetch"
|
||||
2
packages/helper/jest.config.js
Normal file
2
packages/helper/jest.config.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/** @type {import('jest').Config} */
|
||||
module.exports = { ...require("config/jest.config"), preset: "ts-jest", testEnvironment: "node" }
|
||||
18
packages/helper/package.json
Normal file
18
packages/helper/package.json
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"name": "helper",
|
||||
"version": "0.0.0",
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"lint": "TIMING=1 eslint \"**/*.ts*\"",
|
||||
"build": "tsc",
|
||||
"test": "FORCE_COLOR=1 yarn node --experimental-vm-modules $(yarn bin jest --passWithNoTests)"
|
||||
},
|
||||
"dependencies": {
|
||||
"isomorphic-unfetch": "^4.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"config": "*"
|
||||
}
|
||||
}
|
||||
25
packages/helper/tsconfig.json
Normal file
25
packages/helper/tsconfig.json
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"include": ["."],
|
||||
"exclude": ["dist", "build", "node_modules"],
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"strict": true,
|
||||
"removeComments": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"target": "es2017",
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"baseUrl": "./",
|
||||
"incremental": true,
|
||||
"skipLibCheck": true,
|
||||
"strictNullChecks": true,
|
||||
"esModuleInterop": true,
|
||||
"noImplicitAny": false,
|
||||
"strictBindCallApply": false,
|
||||
"forceConsistentCasingInFileNames": false,
|
||||
"noFallthroughCasesInSwitch": false
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue