created HttpRequest
This commit is contained in:
0
src/PCastApi.ts
Normal file
0
src/PCastApi.ts
Normal file
25
src/index.ts
25
src/index.ts
@@ -1 +1,24 @@
|
|||||||
// coming soon...
|
import {HttpRequests} from './net/http/HttpRequests';
|
||||||
|
import {Channels} from './pcast/Channels';
|
||||||
|
|
||||||
|
// coming soon..
|
||||||
|
const applicationCredentials = {
|
||||||
|
id: 'phenixrts.com-alex.zinn',
|
||||||
|
secret: 'AMAsDzr.dIuGMZ.Zu52Dt~MQvP!DZwYg'
|
||||||
|
};
|
||||||
|
const pcastUri = 'https://pcast-stg.phenixrts.com';
|
||||||
|
|
||||||
|
const httpRequests = new HttpRequests(
|
||||||
|
`${pcastUri}/pcast`,
|
||||||
|
new Headers({
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Accept: 'application/json',
|
||||||
|
Authorization: `Basic ${btoa(`${applicationCredentials.id}:${applicationCredentials.secret}`)}`
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const channels = new Channels(httpRequests);
|
||||||
|
|
||||||
|
channels.getChannels().then(channels => {
|
||||||
|
console.log(channels);
|
||||||
|
});
|
||||||
|
|||||||
3
src/lang/assertUnreachable.ts
Normal file
3
src/lang/assertUnreachable.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export default function assertUnreachable(x: never): never {
|
||||||
|
throw new Error(`Unreachable code: ${x}`);
|
||||||
|
}
|
||||||
8
src/net/http/HttpMethod.ts
Normal file
8
src/net/http/HttpMethod.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// Replace entire file with simplified string enum
|
||||||
|
export enum HttpMethod {
|
||||||
|
Get = 'GET',
|
||||||
|
Post = 'POST',
|
||||||
|
Put = 'PUT',
|
||||||
|
Patch = 'PATCH',
|
||||||
|
Delete = 'DELETE'
|
||||||
|
}
|
||||||
58
src/net/http/HttpRequests.ts
Normal file
58
src/net/http/HttpRequests.ts
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import {HttpMethod} from './HttpMethod';
|
||||||
|
|
||||||
|
const defaultRequestTimeoutDurationInMilliseconds = 30_000;
|
||||||
|
|
||||||
|
export class HttpRequests {
|
||||||
|
private readonly _baseUri: string;
|
||||||
|
private readonly _baseHeaders: Headers;
|
||||||
|
private readonly _requestTimeoutDuration: number;
|
||||||
|
|
||||||
|
constructor(baseUri: string, baseHeaders: Headers, options: {requestTimeoutDuration?: number} = {}) {
|
||||||
|
this._baseUri = baseUri;
|
||||||
|
this._baseHeaders = baseHeaders;
|
||||||
|
this._requestTimeoutDuration = options.requestTimeoutDuration ?? defaultRequestTimeoutDurationInMilliseconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async request<T>(method: HttpMethod, path: string, options?: RequestInit & {body?: Record<string, unknown> | string}): Promise<T | void> {
|
||||||
|
const abortController = new AbortController();
|
||||||
|
const abortSignal = abortController.signal;
|
||||||
|
|
||||||
|
let requestOptions: RequestInit = {
|
||||||
|
headers: this._baseHeaders,
|
||||||
|
method: method.toString(), // Convert enum to string
|
||||||
|
signal: abortSignal
|
||||||
|
};
|
||||||
|
|
||||||
|
if (options?.body && method !== HttpMethod.Get) {
|
||||||
|
requestOptions.body = typeof options.body === 'string' ? options.body : JSON.stringify(options.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.makeRequest<T>(path, requestOptions, abortController, this._requestTimeoutDuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async makeRequest<T>(path: string, options: RequestInit, abortController: AbortController, timeoutDuration: number): Promise<T | void> {
|
||||||
|
const requestTimeoutId = globalThis.setTimeout(() => abortController.abort(), timeoutDuration);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const requestPath = `${this._baseUri}${path}`;
|
||||||
|
const response = await fetch(requestPath, options);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status [${response.status}] ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseContentType = response.headers.get('content-type');
|
||||||
|
|
||||||
|
if (responseContentType?.includes('application/json')) {
|
||||||
|
return response.json() as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.text() as T;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
return;
|
||||||
|
} finally {
|
||||||
|
globalThis.clearTimeout(requestTimeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/pcast/Channels.ts
Normal file
14
src/pcast/Channels.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import {HttpMethod} from '../net/http/HttpMethod';
|
||||||
|
import type {HttpRequests} from '../net/http/HttpRequests';
|
||||||
|
|
||||||
|
export class Channels {
|
||||||
|
private readonly _httpRequests: HttpRequests;
|
||||||
|
|
||||||
|
constructor(httpRequests: HttpRequests) {
|
||||||
|
this._httpRequests = httpRequests;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getChannels() {
|
||||||
|
return this._httpRequests.request(HttpMethod.Get, '/channels');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user