Skip to content

Commit

Permalink
improved client
Browse files Browse the repository at this point in the history
  • Loading branch information
golsch committed Feb 5, 2025
1 parent 46643ba commit bd5aef6
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 182 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"prettier": "^3.4.2",
"typescript": "^5.7.2",
"typescript-eslint": "^8.17.0"
},
"dependencies": {
"axios": "^1.7.9"
}
}
27 changes: 18 additions & 9 deletions src/acl/accesskey/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
} from './model';

// TODO fix TOTAL Count
const extractResponse = async (
response: MCRHttpResponse
): Promise<AccessKeyInformation> => {
const extractResponse = (
response: MCRHttpResponse<AccessKeyDto[]>
): AccessKeyInformation => {
console.log(response.headers);
const totalCount = response.headers['X-Total-Count'];
return {
accessKeys: (await response.json()) as AccessKeyDto[],
accessKeys: response.data,
totalCount: totalCount ? parseInt(totalCount, 10) : 0,
};
};
Expand Down Expand Up @@ -46,7 +47,9 @@ export class AccessKeyService {
queryParams.set('limit', String(limit));
}
return extractResponse(
await this.client.getJson(`${API_URL}?${queryParams.toString()}`)
await this.client.get<AccessKeyDto[]>(
`${API_URL}?${queryParams.toString()}`
)
);
}

Expand All @@ -65,15 +68,21 @@ export class AccessKeyService {
if (limit) {
params['limit'] = limit;
}
return extractResponse(await this.client.get(API_URL, params));
return extractResponse(
await this.client.get<AccessKeyDto[]>(API_URL, params)
);
}

public async getAccessKey(id: string): Promise<AccessKeyDto> {
return await this.client.getJson<AccessKeyDto>(`${API_URL}${id}`);
try {
return (await this.client.get<AccessKeyDto>(`${API_URL}${id}`)).data;
} catch {
throw new Error();
}
}

public async createAccessKey(accessKey: CreateAccessKeyDto): Promise<string> {
const response = await this.client.postJson(API_URL, accessKey);
const response = await this.client.post(API_URL, accessKey);
const locationHeader = response.headers['Location'];
return locationHeader.split('/').pop() as string;
}
Expand All @@ -82,7 +91,7 @@ export class AccessKeyService {
id: string,
accessKey: PartialUpdateAccessKeyDto
): Promise<void> {
await this.client.patchJson(`${API_URL}${id}`, accessKey);
await this.client.patch(`${API_URL}${id}`, accessKey);
}

public async deleteAccessKey(id: string): Promise<void> {
Expand Down
Loading

0 comments on commit bd5aef6

Please sign in to comment.