Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
Add fix for instance creation.
Browse files Browse the repository at this point in the history
Applied formatting
Added unit test for instance creation and usage.
  • Loading branch information
envis10n authored and roonie007 committed Jul 13, 2021
1 parent 7af2a68 commit 33e6ffa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
56 changes: 49 additions & 7 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { urlJoin } from "https://deno.land/x/url_join/mod.ts";

import type {
IRequest,
IConfig,
Data,
IAxiodResponse,
} from "./interfaces.ts";
import type { Data, IAxiodResponse, IConfig, IRequest } from "./interfaces.ts";
import { methods } from "./helpers.ts";

function axiod(
Expand All @@ -32,7 +27,54 @@ axiod.create = (config?: IRequest) => {
const instance = Object.assign({}, axiod);
instance.defaults = Object.assign({}, axiod.defaults, config);
instance.defaults.timeout = 1000;

instance.request = (options: IRequest): Promise<IAxiodResponse> => {
return axiod.request(Object.assign({}, instance.defaults, options));
};
instance.get = (url: string, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "get" }),
);
};
instance.post = (url: string, data?: Data, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "post", data }),
);
};
instance.put = (url: string, data?: Data, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "put", data }),
);
};
instance.delete = (url: string, data?: Data, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "delete", data }),
);
};
instance.options = (url: string, data?: Data, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "options", data }),
);
};
instance.head = (url: string, data?: Data, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "head", data }),
);
};
instance.connect = (url: string, data?: Data, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "connect", data }),
);
};
instance.trace = (url: string, data?: Data, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "trace", data }),
);
};
instance.patch = (url: string, data?: Data, config?: IConfig) => {
return instance.request(
Object.assign({}, { url }, config, { method: "patch", data }),
);
};
return instance;
};

Expand Down
7 changes: 7 additions & 0 deletions request-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ Deno.test("Axiod POST request with JSON data", async () => {
assertEquals(data.data.data["foo1"], "bar1");
assertEquals(data.data.data["foo2"], "bar2");
});

Deno.test("Axiod Create baseURL fix", async () => {
const ax = axiod.create({ baseURL: "https://postman-echo.com" });
const data = await ax.post("/post", { foo1: "bar1", foo2: "bar2" });
assertEquals(data.data.data["foo1"], "bar1");
assertEquals(data.data.data["foo2"], "bar2");
});

0 comments on commit 33e6ffa

Please sign in to comment.