Skip to content

Commit

Permalink
refactor: ServiceBuilder methods rename
Browse files Browse the repository at this point in the history
  • Loading branch information
NT committed Jan 2, 2021
1 parent 1d5fac5 commit eb3bddf
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 610 deletions.
669 changes: 96 additions & 573 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/baseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class BaseService {
configurable: true,
get() {
return (...args: any[]) => {
if (serviceBuilder.inlineResponseBody) {
if (serviceBuilder.withInlineResponseBody) {
return self._wrapToInlinedResponse(methodName, args);
} else {
return self._wrapToAxiosResponse(methodName, args);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from "./baseService";
export * from "./decorators";

//export {BaseService} from "./baseService";
12 changes: 6 additions & 6 deletions src/service.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export class ServiceBuilder {
private _standalone: boolean | AxiosInstance = false;
private _requestInterceptors: Array<RequestInterceptorFunction | RequestInterceptor> = [];
private _responseInterceptors: Array<ResponseInterceptorFunction | ResponseInterceptor> = [];
private _inlineResponseBody = false;
private _withInlineResponseBody = false;
private _shouldSaveRequestHistory = false;
private _timeout = 60000;

public build<T>(service: new (builder: ServiceBuilder) => T): T {
return new service(this);
}

public setEndpoint(endpoint: string): ServiceBuilder {
public baseUrl(endpoint: string): ServiceBuilder {
this._endpoint = endpoint;
return this;
}
Expand Down Expand Up @@ -48,8 +48,8 @@ export class ServiceBuilder {
return this;
}

public onlyResponseBody(): ServiceBuilder {
this._inlineResponseBody = true;
public inlineResponseBody(): ServiceBuilder {
this._withInlineResponseBody = true;
return this;
}

Expand All @@ -62,8 +62,8 @@ export class ServiceBuilder {
return this._shouldSaveRequestHistory;
}

get inlineResponseBody(): boolean {
return this._inlineResponseBody;
get withInlineResponseBody(): boolean {
return this._withInlineResponseBody;
}

get endpoint(): string {
Expand Down
2 changes: 1 addition & 1 deletion tests/test/decorators/decorators.formurlencoded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("Decorators - @FormUrlEncoded", () => {
let service: FormUrlEncodedService;

beforeAll(() => {
service = new ServiceBuilder().setEndpoint(testServer.url).build(FormUrlEncodedService);
service = new ServiceBuilder().baseUrl(testServer.url).build(FormUrlEncodedService);
});

const object = {
Expand Down
6 changes: 3 additions & 3 deletions tests/test/decorators/decorators.multipart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FileService, MessagingService } from "../../fixture/fixtures.multipart"

describe("Decorators - Multipart", () => {
test("@Multipart", async () => {
const fileService = new ServiceBuilder().setEndpoint(testServer.url).build(FileService);
const fileService = new ServiceBuilder().baseUrl(testServer.url).build(FileService);
const bucket = {
value: "test-bucket",
};
Expand All @@ -19,15 +19,15 @@ describe("Decorators - Multipart", () => {
});

test("@Multipart - test2", async () => {
const messagingService = new ServiceBuilder().setEndpoint(testServer.url).build(MessagingService);
const messagingService = new ServiceBuilder().baseUrl(testServer.url).build(MessagingService);
const from = { value: "+11111111" };
const to = { value: ["+22222222", "+33333333"] };
const response = await messagingService.createSMS(from, to);
expect(response.config.headers[CONTENT_TYPE_HEADER]).toContain(CONTENT_TYPE.MULTIPART_FORM_DATA);
});

test("@ResponseType", async () => {
const fileService = new ServiceBuilder().setEndpoint(testServer.url).build(FileService);
const fileService = new ServiceBuilder().baseUrl(testServer.url).build(FileService);
const response = await fileService.getFile("x-y-z");
expect(response.config.responseType).toEqual("stream");
});
Expand Down
6 changes: 3 additions & 3 deletions tests/test/decorators/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("Decorators", () => {
let service: PostsApiService;

beforeAll(() => {
service = new ServiceBuilder().setEndpoint(testServer.url).build(PostsApiService);
service = new ServiceBuilder().baseUrl(testServer.url).build(PostsApiService);
});

test("@BasePath", async () => {
Expand All @@ -24,7 +24,7 @@ describe("Decorators", () => {
});

test("@GET - without base path", async () => {
const service = new ServiceBuilder().setEndpoint(testServer.url).build(ServiceWithoutBasePath);
const service = new ServiceBuilder().baseUrl(testServer.url).build(ServiceWithoutBasePath);
const response = await service.get();

expect(response.data).toHaveLength(posts.length);
Expand All @@ -40,7 +40,7 @@ describe("Decorators", () => {
});

test("@GET - Absolute url", async () => {
const service = new ServiceBuilder().setEndpoint(JSONPLACEHOLDER_URL).build(PostsApiService);
const service = new ServiceBuilder().baseUrl(JSONPLACEHOLDER_URL).build(PostsApiService);
const response = await service.getAbsoluteUrl();
expect(response.data).toHaveLength(100);
verifyRequest(response, "get", "/posts");
Expand Down
4 changes: 2 additions & 2 deletions tests/test/decorators/decorators.timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { testServer } from "../../testHelpers";

describe("Decorators - timeout", () => {
test("@Timeout", async () => {
const service = new ServiceBuilder().setEndpoint(testServer.url).build(TimeoutService);
const service = new ServiceBuilder().baseUrl(testServer.url).build(TimeoutService);
await expect(service.timeoutIn3000()).rejects.toThrow(/timeout/);
});

test("The timeout in `@Timeout` decorator should shield the value in `setTimeout` method.", async () => {
const service = new ServiceBuilder().setEndpoint(testServer.url).setTimeout(3000).build(TimeoutService);
const service = new ServiceBuilder().baseUrl(testServer.url).setTimeout(3000).build(TimeoutService);
const response = await service.timeoutIn6000();
expect(response.config.timeout).toEqual(6000);
expect(response.data).toEqual({});
Expand Down
12 changes: 6 additions & 6 deletions tests/test/decorators/decorators.wrong-cases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Post, posts } from "../../fixture/fixtures";

describe("Decorators - wrong cases", () => {
describe("Headers", () => {
const service = new ServiceBuilder().setEndpoint(testServer.url).build(WrongHeaderService);
const service = new ServiceBuilder().baseUrl(testServer.url).build(WrongHeaderService);

describe("@HeaderMap", () => {
test("Empty header key", async () => {
Expand Down Expand Up @@ -54,7 +54,7 @@ describe("Decorators - wrong cases", () => {
});

describe("Fields", () => {
const service = new ServiceBuilder().setEndpoint(testServer.url).build(WrongFieldService);
const service = new ServiceBuilder().baseUrl(testServer.url).build(WrongFieldService);

describe("@FieldMap", () => {
test("Empty field key", async () => {
Expand All @@ -76,7 +76,7 @@ describe("Decorators - wrong cases", () => {
});

describe("Multipart", () => {
const service = new ServiceBuilder().setEndpoint(testServer.url).build(WrongMultipartService);
const service = new ServiceBuilder().baseUrl(testServer.url).build(WrongMultipartService);

test("Empty part key", async () => {
const bucket = {
Expand All @@ -89,7 +89,7 @@ describe("Decorators - wrong cases", () => {
});

describe("Query params", () => {
const service = new ServiceBuilder().setEndpoint(testServer.url).build(WrongQueryService);
const service = new ServiceBuilder().baseUrl(testServer.url).build(WrongQueryService);

describe("@QueryMap", () => {
test("Empty header key", async () => {
Expand Down Expand Up @@ -131,7 +131,7 @@ describe("Decorators - wrong cases", () => {
});

describe("No Http decorator", () => {
const noHttpMethodService = new ServiceBuilder().setEndpoint(testServer.url).build(NoHttpMethodService);
const noHttpMethodService = new ServiceBuilder().baseUrl(testServer.url).build(NoHttpMethodService);

describe("Works fine with not decorated methods", () => {
test("No params", async () => {
Expand Down Expand Up @@ -190,7 +190,7 @@ describe("Decorators - wrong cases", () => {
});

test("__getLastRequest - empty", async () => {
const anyService = new ServiceBuilder().setEndpoint(testServer.url).build(NoHttpMethodService);
const anyService = new ServiceBuilder().baseUrl(testServer.url).build(NoHttpMethodService);
await verifyErrorThrown(async () => {
await anyService.__getLastRequest();
}, ErrorMessages.__TEST_NO_REQUESTS_IN_HISTORY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe("Decorators - inlined response", () => {
beforeEach(() => {
service = new ServiceBuilder()
.saveRequestHistory()
.setEndpoint(testServer.url)
.onlyResponseBody()
.baseUrl(testServer.url)
.inlineResponseBody()
.build(ResponseBodyPostsApiService);
});

Expand All @@ -40,7 +40,7 @@ describe("Decorators - inlined response", () => {
});

test("@GET - without base path", async () => {
const service = new ServiceBuilder().setEndpoint(testServer.url).build(ServiceWithoutBasePath);
const service = new ServiceBuilder().baseUrl(testServer.url).build(ServiceWithoutBasePath);
const response = await service.get();

expect(response.data).toHaveLength(posts.length);
Expand All @@ -56,7 +56,7 @@ describe("Decorators - inlined response", () => {
});

test("@GET - Absolute url", async () => {
const service = new ServiceBuilder().setEndpoint(JSONPLACEHOLDER_URL).build(PostsApiService);
const service = new ServiceBuilder().baseUrl(JSONPLACEHOLDER_URL).build(PostsApiService);
const response = await service.getAbsoluteUrl();
expect(response.data).toHaveLength(100);
verifyRequest(response, "get", "/posts");
Expand Down
2 changes: 1 addition & 1 deletion tests/test/interceptors/request-interceptors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Request interceptors", () => {
});

async function verifyInterceptor(setInterceptor: (builder: ServiceBuilder) => void) {
const builder = new ServiceBuilder().setEndpoint(testServer.url);
const builder = new ServiceBuilder().baseUrl(testServer.url);
setInterceptor(builder);
const service = builder.build(PostsApiService);

Expand Down
6 changes: 3 additions & 3 deletions tests/test/interceptors/response-interceptors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("Response interceptors", () => {
const spy = jest.spyOn(interceptor, "onRejected");

const service = new ServiceBuilder()
.setEndpoint(testServer.url)
.baseUrl(testServer.url)
.setResponseInterceptors(interceptor)
.setStandalone(true)
.build(PostsApiService);
Expand All @@ -69,7 +69,7 @@ describe("Response interceptors", () => {
const spy = jest.spyOn(interceptor, "onRejected");

const service = new ServiceBuilder()
.setEndpoint(testServer.url)
.baseUrl(testServer.url)
.setStandalone(true)
.setResponseInterceptors(interceptor)
.build(PostsApiService);
Expand All @@ -81,7 +81,7 @@ describe("Response interceptors", () => {
});

async function verifyInterceptor(setInterceptor: (builder: ServiceBuilder) => void) {
const builder = new ServiceBuilder().setEndpoint(testServer.url).setStandalone(true);
const builder = new ServiceBuilder().baseUrl(testServer.url).setStandalone(true);

setInterceptor(builder);

Expand Down
2 changes: 1 addition & 1 deletion tests/test/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("Metadata", () => {
let service: PostsApiService;

beforeAll(() => {
service = new ServiceBuilder().setEndpoint(testServer.url).build(PostsApiService);
service = new ServiceBuilder().baseUrl(testServer.url).build(PostsApiService);
});

test("Method not found", () => {
Expand Down
8 changes: 4 additions & 4 deletions tests/test/standalone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import axios, { AxiosRequestConfig } from "axios";

describe("Standalone", () => {
test("With instance", async () => {
const serviceWithoutStandalone = new ServiceBuilder().setEndpoint(testServer.url).build(PostsApiService);
const serviceWithoutStandalone = new ServiceBuilder().baseUrl(testServer.url).build(PostsApiService);
const axiosInstance = axios.create();

axiosInstance.interceptors.response.use((value) => {
Expand All @@ -14,7 +14,7 @@ describe("Standalone", () => {
});

const serviceWithStandalone = new ServiceBuilder()
.setEndpoint(testServer.url)
.baseUrl(testServer.url)
.setStandalone(axiosInstance)
.build(PostsApiService);

Expand All @@ -23,15 +23,15 @@ describe("Standalone", () => {
});

test("With boolean", async () => {
const serviceWithoutStandalone = new ServiceBuilder().setEndpoint(testServer.url).build(PostsApiService);
const serviceWithoutStandalone = new ServiceBuilder().baseUrl(testServer.url).build(PostsApiService);

const interceptor = (config: AxiosRequestConfig) => {
config["standaloneId"] = 101;
return config;
};

const serviceWithStandalone = new ServiceBuilder()
.setEndpoint(testServer.url)
.baseUrl(testServer.url)
.setStandalone(true)
.setRequestInterceptors(interceptor)
.build(PostsApiService);
Expand Down
2 changes: 1 addition & 1 deletion tests/test/transformers/request.transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("Request transformer", () => {
};

const service = new ServiceBuilder()
.setEndpoint(testServer.url)
.baseUrl(testServer.url)
.setRequestInterceptors(interceptor)
.build(TransformerApiService);

Expand Down
2 changes: 1 addition & 1 deletion tests/test/transformers/response.transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("Response transformer", () => {
let service: TransformerApiService;

beforeAll(() => {
service = new ServiceBuilder().setEndpoint(testServer.url).build(TransformerApiService);
service = new ServiceBuilder().baseUrl(testServer.url).build(TransformerApiService);
});

test("@ResponseTransformer", async () => {
Expand Down

0 comments on commit eb3bddf

Please sign in to comment.