Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHPResponse.forHttpCode() shorthand #1322

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/php-wasm/universal/src/lib/php-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ export interface PHPResponseData {
readonly httpStatusCode: number;
}

const responseTexts: Record<number, string> = {
500: 'Internal Server Error',
502: 'Bad Gateway',
404: 'Not Found',
403: 'Forbidden',
401: 'Unauthorized',
400: 'Bad Request',
301: 'Moved Permanently',
302: 'Found',
307: 'Temporary Redirect',
308: 'Permanent Redirect',
204: 'No Content',
201: 'Created',
200: 'OK',
};

/**
* PHP response. Body is an `ArrayBuffer` because it can
* contain binary data.
Expand Down Expand Up @@ -68,6 +84,16 @@ export class PHPResponse implements PHPResponseData {
this.errors = errors;
}

static forHttpCode(httpStatusCode: number, text = '') {
return new PHPResponse(
httpStatusCode,
{},
new TextEncoder().encode(
text || responseTexts[httpStatusCode] || ''
)
);
}

static fromRawData(data: PHPResponseData): PHPResponse {
return new PHPResponse(
data.httpStatusCode,
Expand Down
Loading