Skip to content

Commit

Permalink
fix: Harmonize services signatures
Browse files Browse the repository at this point in the history
BREAKING: Changes the opts in different services to accept key basePath instead of path
  • Loading branch information
dnlkoch committed Jun 20, 2022
1 parent 720b38b commit 749b731
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/service/AppInfoService/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('AppInfoService', () => {
expect(AppInfoService).toBeDefined();
});

it('findAll GET', async () => {
it('has set the correct defaults (getAppInfo)', async () => {
fetchMock = fetchSpy(successResponse([]));

await service.getAppInfo();
Expand Down
10 changes: 5 additions & 5 deletions src/service/AppInfoService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';
import { AppInfo } from '../../model/AppInfo';

export interface AppInfoServiceOpts {
url: string;
basePath: string;
};

export class AppInfoService {
private path: string;
private basePath: string;

constructor(opts: AppInfoServiceOpts = {
url: '/info/app'
basePath: '/info'
}) {
this.path = opts.url;
this.basePath = opts.basePath;
}

async getAppInfo(fetchOpts?: RequestInit): Promise<AppInfo> {
try {
const response = await fetch(this.path, {
const response = await fetch(`${this.basePath}/app`, {
method: 'GET',
headers: {
...getCsrfTokenHeader()
Expand Down
16 changes: 14 additions & 2 deletions src/service/AuthService/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';

export interface AuthServiceOpts {
basePath: string;
};

export class AuthService {
async logout(url: string = '/sso/logout', requestOpts?: RequestInit): Promise<void> {
private basePath: string;

constructor(opts: AuthServiceOpts = {
basePath: '/sso'
}) {
this.basePath = opts.basePath;
}

async logout(requestOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(url, {
const response = await fetch(`${this.basePath}/logout`, {
method: 'POST',
credentials: 'same-origin',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/service/GenericService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface GenericServiceOpts {

export abstract class GenericService<T extends BaseEntity> {

private basePath: string;
basePath: string;

constructor(opts: GenericServiceOpts) {
this.basePath = opts.basePath;
Expand Down
10 changes: 5 additions & 5 deletions src/service/GraphQLService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ export interface GraphQLResponse<T> {
};

export interface GraphQLServiceOpts {
url: string;
basePath: string;
};

export class GraphQLService {

private url: string;
private basePath: string;

constructor(opts: GraphQLServiceOpts = {
url: '/graphql'
basePath: '/graphql'
}) {
this.url = opts.url;
this.basePath = opts.basePath;
}

async sendQuery<T>(query: GraphQLQueryObject, fetchOpts?: RequestInit): Promise<GraphQLResponse<T>> {
try {
const response = await fetch(this.url, {
const response = await fetch(this.basePath, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
12 changes: 9 additions & 3 deletions src/service/SHOGunClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export class SHOGunClient {
}

info() {
return new AppInfoService();
return new AppInfoService({
basePath: `${this.basePath}info`
});
}

application<T extends Application>() {
Expand All @@ -49,11 +51,15 @@ export class SHOGunClient {
}

auth() {
return new AuthService();
return new AuthService({
basePath: `${this.basePath}sso`
});
}

graphql() {
return new GraphQLService();
return new GraphQLService({
basePath: `${this.basePath}graphql`
});
}

}
Expand Down

0 comments on commit 749b731

Please sign in to comment.