Skip to content
This repository has been archived by the owner on Jan 29, 2021. It is now read-only.

Commit

Permalink
- サービスインスタンスに対してプロジェクトを指定できるように調整
Browse files Browse the repository at this point in the history
  • Loading branch information
ilovegadd committed Jan 12, 2021
1 parent f0374fc commit e49fd07
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

### Changed

- サービスインスタンスに対してプロジェクトを指定できるように調整

### Deprecated

### Removed
Expand Down
40 changes: 20 additions & 20 deletions src/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,8 @@ import * as sinon from 'sinon';

import { StubAuthClient } from './auth/authClient';
import { Service } from './service';
import { DefaultTransporter, Transporter } from './transporters';
import { DefaultTransporter, StubTransporter } from './transporters';

/**
* スタブトランポーター
*/
class StubTransporter implements Transporter {
public body: any;
constructor(body: any) {
this.body = body;
}
public async fetch(_: string, options: RequestInit) {
return new Response(this.body, options);
}
}
const API_ENDPOINT = 'https://example.com';

describe('fetch()', () => {
Expand All @@ -42,10 +30,14 @@ describe('fetch()', () => {
const auth = new StubAuthClient();
const service = new Service({
auth: auth,
endpoint: API_ENDPOINT
endpoint: API_ENDPOINT,
project: { id: 'xxx' }
});

sandbox.mock(service.options.auth).expects('fetch').once().resolves(response);
sandbox.mock(service.options.auth)
.expects('fetch')
.once()
.resolves(response);

const result = await service.fetch(<any>{});

Expand Down Expand Up @@ -83,8 +75,11 @@ describe('fetch()', () => {
endpoint: API_ENDPOINT
});

sandbox.mock(service.options.auth).expects('fetch').once()
.withArgs(sinon.match(new RegExp(`\\?${querystrings}$`))).resolves(response);
sandbox.mock(service.options.auth)
.expects('fetch')
.once()
.withArgs(sinon.match(new RegExp(`\\?${querystrings}$`)))
.resolves(response);

const result = await service.fetch(<any>options);
assert.deepEqual(result, response);
Expand All @@ -108,8 +103,11 @@ describe('fetch()', () => {
endpoint: API_ENDPOINT
});

sandbox.mock(service.options.auth).expects('fetch').once()
.withArgs(sinon.match(new RegExp(`\\?${querystrings}$`))).resolves(response);
sandbox.mock(service.options.auth)
.expects('fetch')
.once()
.withArgs(sinon.match(new RegExp(`\\?${querystrings}$`)))
.resolves(response);

const result = await service.fetch(<any>options);

Expand All @@ -123,7 +121,9 @@ describe('fetch()', () => {
endpoint: API_ENDPOINT
});

sandbox.mock(DefaultTransporter.prototype).expects('fetch').once();
sandbox.mock(DefaultTransporter.prototype)
.expects('fetch')
.once();

await service.fetch(<any>options);
sandbox.verify();
Expand Down
19 changes: 18 additions & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export interface IOptions {
* transporter object
*/
transporter?: Transporter;
/**
* サービスを使用するプロジェクト
*/
project?: { id?: string };
}

export interface IFetchOptions {
uri: string;
form?: any;
Expand All @@ -33,14 +38,17 @@ export interface IFetchOptions {
body?: any;
expectedStatusCodes: number[];
}

/**
* base service class
*/
export class Service {
public options: IOptions;

constructor(options: IOptions) {
this.options = options;
}

/**
* Create and send request to API
*/
Expand All @@ -52,7 +60,15 @@ export class Service {
// tslint:disable-next-line:no-parameter-reassignment
options = { ...defaultOptions, ...options };

const baseUrl = this.options.endpoint;
let baseUrl = this.options.endpoint;
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (this.options.project !== undefined
&& this.options.project !== null
&& typeof this.options.project.id === 'string'
&& this.options.project.id.length > 0) {
baseUrl = `${baseUrl}/projects/${this.options.project.id}`;
}
let url = `${baseUrl}${options.uri}`;

const querystrings = qs.stringify(options.qs);
Expand Down Expand Up @@ -83,6 +99,7 @@ export class Service {
}
}
}

/**
* 検索結果インターフェース
*/
Expand Down
18 changes: 16 additions & 2 deletions src/transporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const debug = createDebug('ttts-api-abstract-client:transporters');
* トランスポーター抽象クラス
*/
export abstract class Transporter {
public abstract async fetch(url: string, options: RequestInit): Promise<Response>;
public abstract fetch(url: string, options: RequestInit): Promise<Response>;
}
export type IBodyResponseCallback = Promise<Response>;
/**
Expand All @@ -29,7 +29,6 @@ export class RequestError extends Error {
this.name = 'TTTSRequestError';
}
}

/**
* DefaultTransporter
*/
Expand Down Expand Up @@ -102,3 +101,18 @@ export class DefaultTransporter implements Transporter {
throw err;
}
}

/**
* スタブトランポーター
*/
export class StubTransporter implements Transporter {
public body: any;

constructor(body: any) {
this.body = body;
}

public async fetch(_: string, options: RequestInit) {
return new Response(this.body, options);
}
}

0 comments on commit e49fd07

Please sign in to comment.