Skip to content

Commit

Permalink
Refactored axios client generation to expose a configurable class (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfportal authored Dec 13, 2018
1 parent d8f02da commit 9759418
Show file tree
Hide file tree
Showing 5 changed files with 496 additions and 399 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ This is work in progress as of 14 Nov 2018:
[![License](https://img.shields.io/npm/l/@airtasker/spot.svg)](https://github.com/airtasker/spot/blob/master/package.json)

<!-- toc -->

- [Spot](#spot)
- [Usage](#usage)
- [Commands](#commands)
<!-- tocstop -->
* [Spot](#spot)
* [Usage](#usage)
* [Commands](#commands)
<!-- tocstop -->

# Usage

Expand Down Expand Up @@ -372,11 +371,10 @@ Define a default error for the endpoint. This can only be used once for an `@end
# Commands

<!-- commands -->

- [`spot generate`](#spot-generate)
- [`spot help [COMMAND]`](#spot-help-command)
- [`spot init`](#spot-init)
- [`spot validate SPOT_CONTRACT`](#spot-validate-spot-contract)
* [`spot generate`](#spot-generate)
* [`spot help [COMMAND]`](#spot-help-command)
* [`spot init`](#spot-init)
* [`spot validate SPOT_CONTRACT`](#spot-validate-spot-contract)

## `spot generate`

Expand Down
77 changes: 48 additions & 29 deletions clients/axios/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createUser, deleteUser, findUsers, getUser } from "./sdk/client";
import { SpotApi } from "./sdk/client";
import * as moxios from "moxios";
import { CreateUserResponse } from "./sdk/types";

Expand All @@ -7,6 +7,8 @@ const createUserResponse: CreateUserResponse = {
created_at: "2018-01-01"
};

const configuredApi = new SpotApi({ baseUrl: "http://localhost:9999/api" });

describe("TypeScript axios client sdk test", () => {
beforeEach(() => {
moxios.install();
Expand All @@ -23,10 +25,13 @@ describe("TypeScript axios client sdk test", () => {
response: createUserResponse
});

await createUser({ name: "User 1", roles: "admin" }, "test-token");
await configuredApi.createUser(
{ name: "User 1", roles: "admin" },
"test-token"
);

const request = moxios.requests.mostRecent();
expect(request.config.url).toBe("/users/create");
expect(request.config.url).toBe("http://localhost:9999/api/users/create");
});

it("passes the correct method and request body", async () => {
Expand All @@ -35,7 +40,10 @@ describe("TypeScript axios client sdk test", () => {
response: createUserResponse
});

await createUser({ name: "User 1", roles: "admin" }, "test-token");
await configuredApi.createUser(
{ name: "User 1", roles: "admin" },
"test-token"
);

const request = moxios.requests.mostRecent();
expect(request.config.method).toBe("post");
Expand All @@ -51,7 +59,10 @@ describe("TypeScript axios client sdk test", () => {
response: createUserResponse
});

await createUser({ name: "User 1", roles: "admin" }, "test-token");
await configuredApi.createUser(
{ name: "User 1", roles: "admin" },
"test-token"
);

const request = moxios.requests.mostRecent();
expect(request.config.method).toBe("post");
Expand All @@ -64,7 +75,10 @@ describe("TypeScript axios client sdk test", () => {
response: createUserResponse
});

await createUser({ name: "User 1", roles: "admin" }, undefined);
await configuredApi.createUser(
{ name: "User 1", roles: "admin" },
undefined
);
const request = moxios.requests.mostRecent();
expect(request.config.method).toBe("post");
expect(request.config.headers["Authorization"]).toBeUndefined();
Expand All @@ -76,7 +90,7 @@ describe("TypeScript axios client sdk test", () => {
response: createUserResponse
});

const response = await createUser(
const response = await configuredApi.createUser(
{ name: "User 1", roles: "admin" },
"test-token"
);
Expand All @@ -91,7 +105,10 @@ describe("TypeScript axios client sdk test", () => {
});

try {
await createUser({ name: "User 1", roles: "admin" }, "test-token");
await configuredApi.createUser(
{ name: "User 1", roles: "admin" },
"test-token"
);
} catch (e) {
expect(e).toEqual(
new Error("Invalid response for successful status code: {}")
Expand All @@ -104,7 +121,7 @@ describe("TypeScript axios client sdk test", () => {
status: 400
});

const response = await createUser(
const response = await configuredApi.createUser(
{ name: "User 1", roles: "admin" },
"test-token"
);
Expand All @@ -125,10 +142,10 @@ describe("TypeScript axios client sdk test", () => {
]
});

await findUsers(10, "user");
await configuredApi.findUsers(10, "user");

const request = moxios.requests.mostRecent();
expect(request.config.url).toBe("/users");
expect(request.config.url).toBe("http://localhost:9999/api/users");
});

it("passes the correct method and queryParams", async () => {
Expand All @@ -142,7 +159,7 @@ describe("TypeScript axios client sdk test", () => {
]
});

await findUsers(10, "user");
await configuredApi.findUsers(10, "user");

const request = moxios.requests.mostRecent();
expect(request.config.method).toBe("get");
Expand All @@ -164,7 +181,7 @@ describe("TypeScript axios client sdk test", () => {
]
});

await findUsers(10, undefined);
await configuredApi.findUsers(10, undefined);

const request = moxios.requests.mostRecent();
expect(request.config.method).toBe("get");
Expand All @@ -191,7 +208,7 @@ describe("TypeScript axios client sdk test", () => {
response: expected
});

const response = await findUsers(10, "user");
const response = await configuredApi.findUsers(10, "user");
expect(response.kind).toBe("success");
expect(response.data).toEqual(expected);
});
Expand All @@ -211,7 +228,7 @@ describe("TypeScript axios client sdk test", () => {
response: expected
});

const response = await findUsers(10, "user");
const response = await configuredApi.findUsers(10, "user");
expect(response.kind).toBe("success");
expect(response.data).toEqual(expected);
});
Expand All @@ -221,7 +238,7 @@ describe("TypeScript axios client sdk test", () => {
status: 400
});

const response = await findUsers(10, "user");
const response = await configuredApi.findUsers(10, "user");
expect(response.kind).toBe("unknown-error");
expect(response.data).toBeUndefined();
});
Expand All @@ -237,11 +254,11 @@ describe("TypeScript axios client sdk test", () => {
}
});

await getUser(123);
await configuredApi.getUser(123);

const request = moxios.requests.mostRecent();
expect(request.config.method).toBe("get");
expect(request.config.url).toBe("/users/123");
expect(request.config.url).toBe("http://localhost:9999/api/users/123");
});

it("can handle successful request", async () => {
Expand All @@ -254,7 +271,7 @@ describe("TypeScript axios client sdk test", () => {
response: expected
});

const response = await getUser(123);
const response = await configuredApi.getUser(123);
expect(response.kind).toBe("success");
expect(response.data).toEqual(expected);
});
Expand All @@ -268,7 +285,7 @@ describe("TypeScript axios client sdk test", () => {
response: expected
});

const response = await getUser(123);
const response = await configuredApi.getUser(123);
expect(response.kind).toBe("success");
expect(response.data).toEqual(expected);
});
Expand All @@ -285,7 +302,7 @@ describe("TypeScript axios client sdk test", () => {
});

try {
await getUser(123);
await configuredApi.getUser(123);
} catch (e) {
expect(e).toEqual(
new Error(
Expand All @@ -304,7 +321,7 @@ describe("TypeScript axios client sdk test", () => {
status: 400
});

const response = await getUser(123);
const response = await configuredApi.getUser(123);
expect(response.kind).toBe("unknown-error");
expect(response.data).toBeUndefined();
});
Expand All @@ -317,11 +334,13 @@ describe("TypeScript axios client sdk test", () => {
response: null
});

await deleteUser(123, "test-token");
await configuredApi.deleteUser(123, "test-token");

const request = moxios.requests.mostRecent();
expect(request.config.method).toBe("delete");
expect(request.config.url).toBe("/users/123-confirmed");
expect(request.config.url).toBe(
"http://localhost:9999/api/users/123-confirmed"
);
});

it("passes the correct header", async () => {
Expand All @@ -330,7 +349,7 @@ describe("TypeScript axios client sdk test", () => {
response: null
});

await deleteUser(123, "test-token");
await configuredApi.deleteUser(123, "test-token");

const request = moxios.requests.mostRecent();
expect(request.config.headers["Authorization"]).toBe("test-token");
Expand All @@ -342,7 +361,7 @@ describe("TypeScript axios client sdk test", () => {
response: null
});

const response = await deleteUser(123, "test-token");
const response = await configuredApi.deleteUser(123, "test-token");
expect(response.kind).toBe("success");
expect(response.data).toEqual(null);
});
Expand All @@ -354,7 +373,7 @@ describe("TypeScript axios client sdk test", () => {
});

try {
await deleteUser(123, "test-token");
await configuredApi.deleteUser(123, "test-token");
} catch (e) {
expect(e).toEqual(
new Error("Invalid response for successful status code: {}")
Expand All @@ -372,7 +391,7 @@ describe("TypeScript axios client sdk test", () => {
response: expected
});

const response = await deleteUser(123, "test-token");
const response = await configuredApi.deleteUser(123, "test-token");
expect(response.kind).toBe("forbidden");
expect(response.data).toStrictEqual(expected);
});
Expand All @@ -387,7 +406,7 @@ describe("TypeScript axios client sdk test", () => {
});

try {
await deleteUser(123, "test-token");
await configuredApi.deleteUser(123, "test-token");
} catch (e) {
expect(e).toEqual(
new Error(
Expand Down
Loading

0 comments on commit 9759418

Please sign in to comment.