Skip to content

Commit

Permalink
fix: command set method
Browse files Browse the repository at this point in the history
  • Loading branch information
Fjandin committed Nov 7, 2024
1 parent b8c55c3 commit 0d74a68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/common/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Client {
const request = command.getRequest()
const authToken = await this.getAuthToken?.()
const response = await fetch(request.baseUrl + request.path, {
method: "POST",
method: request.method,
headers: {
...request.headers,
...(authToken ? { Authorization: `Bearer ${authToken}` } : {}),
Expand Down
23 changes: 17 additions & 6 deletions src/common/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export abstract class Command<Input, Output> {
*/
protected abstract getBaseUrl(): string

/**
* Get the method for the request
*/
protected getMethod(): string {
return "POST"
}

/**
* Get the path for the request
*/
Expand All @@ -29,17 +36,19 @@ export abstract class Command<Input, Output> {
/**
* Get the body for the request
*/
protected getBody(): string {
return JSON.stringify(this.input)
protected getBody(): string | undefined {
return this.input ? JSON.stringify(this.input) : undefined
}

/**
* Get the headers for the request
*/
protected getHeaders(): Record<string, string> {
return {
"Content-Type": "application/json",
}
return this.getBody()
? {
"Content-Type": "application/json",
}
: {}
}

/**
Expand All @@ -51,17 +60,19 @@ export abstract class Command<Input, Output> {
* Get the request object
*/
public getRequest(): {
body: string
body: string | undefined
headers: Record<string, string>
baseUrl: string
path: string
method: string
parseResponse: (response: unknown) => Output
} {
return {
body: this.getBody(),
headers: this.getHeaders(),
baseUrl: this.getBaseUrl(),
path: this.getPath(),
method: this.getMethod(),
parseResponse: this.parseResponse.bind(this),
}
}
Expand Down

0 comments on commit 0d74a68

Please sign in to comment.