-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add support for raw requests (#25)
* Added useRawRequests new hook for requests outside of Api * Refactor service to fix issue with response clone and highWaterMark * Removed type, moved to method signature instead * Refactor service fixing bug with url, implemented raw requests * Updated tests * Removed unused mock * Added tests cases for useRawRequests * Added more test for useRequests * Throw an error when useRequests is called without initializing context first * Fixed tests for init * Fix path to use mapped instead of literal absolute import * Included set of examples * Updated gitignore * Added dev dependency for testing locally, ts-node * Updated documentation * Remove comment from readme * Improve readme
- Loading branch information
Showing
17 changed files
with
505 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
dist/ | ||
node_modules/ | ||
run.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,39 @@ | ||
import { init } from "@/init"; | ||
import { useRequest } from "@/useRequests"; | ||
import { Service } from "@/service"; | ||
import { Options } from "@/options"; | ||
import Service from "@/service"; | ||
import useRequest from "@/useRequests"; | ||
|
||
// jest.mock("./service"); | ||
// jest.mock("./options"); | ||
enum Test { | ||
endpoint1 = "/endpoint1", | ||
endpoint2 = "/endpoint2", | ||
endpoint3 = "/endpoint3", | ||
} | ||
|
||
describe("init", () => { | ||
const baseURL = "http://api.example.io"; | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
// delete globalThis.services; | ||
init(baseURL, { ...Test }); | ||
}); | ||
|
||
it("should initialize options correctly", () => { | ||
// const mockInstance = { opts: {} }; | ||
// (Options.instance as jest.Mock).mockReturnValue(mockInstance); | ||
const base = "http://myapi.io/test"; | ||
enum endpoints { | ||
endpoint1 = "/endpoint1", | ||
} | ||
init(base, { ...endpoints }); | ||
// expect(Options.instance).toHaveBeenCalled(); | ||
// expect(mockInstance.opts).toEqual({ base }); | ||
it("should set the baseURL correctly", () => { | ||
expect(Options.instance().baseURL).toBe(baseURL); | ||
}); | ||
|
||
// it("should create services correctly", () => { | ||
// const base = "http://example.com"; | ||
// const endpoints = { endpoint1: "/api/endpoint1" }; | ||
it("should create services correctly", () => { | ||
const ctx = useRequest<typeof Test>(); | ||
expect(ctx.endpoint1).toBeDefined(); | ||
expect(ctx.endpoint1).toBeInstanceOf(Service); | ||
|
||
// init(base, endpoints); | ||
expect(ctx.endpoint2).toBeDefined(); | ||
expect(ctx.endpoint2).toBeInstanceOf(Service); | ||
|
||
// expect(Service).toHaveBeenCalledWith("/api/endpoint1"); | ||
// }); | ||
|
||
// it("should define globalThis.services correctly", () => { | ||
// const base = "http://example.com"; | ||
// const endpoints = { endpoint1: "/api/endpoint1" }; | ||
|
||
// init(base, endpoints); | ||
|
||
// expect(globalThis.services).toBeDefined(); | ||
// expect(globalThis.services.endpoint1).toBeInstanceOf(Service); | ||
// }); | ||
|
||
// it("should not redefine globalThis.services if already defined", () => { | ||
// globalThis.services = { existingService: "existing" }; | ||
|
||
// const base = "http://example.com"; | ||
// const endpoints = { endpoint1: "/api/endpoint1" }; | ||
|
||
// init(base, endpoints); | ||
expect(ctx.endpoint3).toBeDefined(); | ||
expect(ctx.endpoint3).toBeInstanceOf(Service); | ||
}); | ||
|
||
// expect(globalThis.services).toEqual({ existingService: "existing" }); | ||
// }); | ||
it("should define globalThis.useRequests correctly", () => { | ||
// @ts-ignore | ||
expect(globalThis.useRequests).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.