Fetch based client implemented

Former-commit-id: cabdae0c9e
restapi
Jens Pelzetter 2020-07-13 19:19:27 +02:00
parent 5e4d73b44d
commit 4e046647c7
2 changed files with 151 additions and 18 deletions

View File

@ -1,3 +1,3 @@
dist dist
node_modules node_modules
target

View File

@ -25,7 +25,7 @@ export interface LibreCcmApiClient {
*/ */
post( post(
endpoint: string, endpoint: string,
body: unknown, body: RequestBody,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse>; ): Promise<ApiResponse>;
/** /**
@ -39,7 +39,7 @@ export interface LibreCcmApiClient {
*/ */
put( put(
endpoint: string, endpoint: string,
body: Record<string, unknown>, body: unknown,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse>; ): Promise<ApiResponse>;
/** /**
@ -72,6 +72,12 @@ export interface LibreCcmApiClient {
options(endpoint: string): Promise<ApiResponse>; options(endpoint: string): Promise<ApiResponse>;
} }
export type RequestBody =
| Record<string, unknown>
| ArrayBuffer
| string
| undefined;
/** /**
* The response from the API. * The response from the API.
*/ */
@ -194,11 +200,22 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
} }
private buildFetchOptions( private buildFetchOptions(
method: "get" | "post" | "put" | "delete" | "head" | "options" method: "get" | "post" | "put" | "delete" | "head" | "options",
body?: RequestBody
): RequestInit { ): RequestInit {
const fetchOptions: RequestInit = {}; const fetchOptions: RequestInit = {
Object.assign(fetchOptions, this.#fetchOptions); ...this.#fetchOptions,
};
fetchOptions.method = method; fetchOptions.method = method;
if (body) {
if (body instanceof ArrayBuffer) {
fetchOptions.body = body;
} else if (typeof body === "string") {
fetchOptions.body = body;
} else {
fetchOptions.body = JSON.stringify(body);
}
}
return fetchOptions; return fetchOptions;
} }
@ -232,35 +249,151 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
} }
} }
post( async post(
endpoint: string, endpoint: string,
body: unknown, body: RequestBody,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse> { ): Promise<ApiResponse> {
throw "Not implemented yet."; const url = buildUrl(this.#baseUrl, endpoint, searchParams);
try {
const response = await fetch(
url,
this.buildFetchOptions("post", body)
);
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
url,
};
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
url,
};
}
} }
put( async put(
endpoint: string, endpoint: string,
body: unknown, body: RequestBody,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse> { ): Promise<ApiResponse> {
throw "Not implemented yet."; const url = buildUrl(this.#baseUrl, endpoint, searchParams);
try {
const response = await fetch(
url,
this.buildFetchOptions("put", body)
);
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
url,
};
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
url,
};
}
} }
delete( async delete(
endpoint: string, endpoint: string,
searchParams?: Record<string, string> searchParams?: Record<string, string>
): Promise<ApiResponse> { ): Promise<ApiResponse> {
throw "Not implemented yet."; const url = buildUrl(this.#baseUrl, endpoint, searchParams);
try {
const response = await fetch(url, this.buildFetchOptions("delete"));
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
url,
};
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
url,
};
}
} }
head(endpoint: string): Promise<ApiResponse> { async head(endpoint: string): Promise<ApiResponse> {
throw "Not implemented yet."; const url = buildUrl(this.#baseUrl, endpoint);
try {
const response = await fetch(url, this.buildFetchOptions("head"));
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
url,
};
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
url,
};
}
} }
options(endpoint: string): Promise<ApiResponse> { async options(endpoint: string): Promise<ApiResponse> {
throw "Not implemented yet."; const url = buildUrl(this.#baseUrl, endpoint);
try {
const response = await fetch(url, this.buildFetchOptions("options"));
if (response.ok) {
return new FetchResponse(response);
} else {
throw {
status: response.status,
statusText: response.statusText,
method: "get",
message: "API responded with an error.",
url,
};
}
} catch (err) {
throw {
status: -1,
statusText: "n/a",
method: "get",
message: `Failed to execute get: ${err}`,
url,
};
}
} }
} }