Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jayan-blutui committed Nov 14, 2024
1 parent 551615c commit 2e05703
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class Client {
}
}

/**
* Perform a `GET` request to the given path.
*/
async get(
path: string,
options: { params?: Record<string, unknown>; headers?: HeadersInit }
Expand All @@ -32,6 +35,9 @@ export class Client {
})
}

/**
* Perform a `POST` request to the given path with the given payload.
*/
async post<Entity>(
path: string,
entity: Entity,
Expand All @@ -46,6 +52,9 @@ export class Client {
})
}

/**
* Perform a `PATCH` request to the given path with the given payload.
*/
async patch<Entity>(
path: string,
entity: Entity,
Expand All @@ -60,6 +69,9 @@ export class Client {
})
}

/**
* Perform a `DELETE` request to the given path.
*/
async delete(
path: string,
options: { params?: Record<string, unknown>; headers?: HeadersInit }
Expand All @@ -72,6 +84,9 @@ export class Client {
})
}

/**
* Get the resource URL for the given path.
*/
private getResourceURL(path: string, params?: Record<string, unknown>) {
const queryString = getQueryString(params)
const url = new URL(
Expand All @@ -93,6 +108,9 @@ export class Client {
return `${version}/${newPath}`
}

/**
* Perform a request with the given options.
*/
private async fetch(url: string, options?: RequestInit) {
const response = await this._fetchFn(url, {
...this.options,
Expand Down Expand Up @@ -127,6 +145,9 @@ export class Client {
}
}

/**
* Get the query string.
*/
function getQueryString(queryObj?: Record<string, unknown>) {
if (!queryObj) return undefined

Expand All @@ -146,6 +167,9 @@ function getQueryString(queryObj?: Record<string, unknown>) {
return new URLSearchParams(sanitizedQueryObj).toString()
}

/**
* Get the body as a JSON string.
*/
function getBody<Entity>(entity: Entity): BodyInit | null | undefined {
return JSON.stringify(entity)
}

0 comments on commit 2e05703

Please sign in to comment.