@azure/functions
Version:
Microsoft Azure Functions NodeJS Framework
93 lines (75 loc) • 3.06 kB
text/typescript
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.
import * as types from '@azure/functions';
import { HttpResponseInit } from '@azure/functions';
import { Blob } from 'buffer';
import { ReadableStream } from 'stream/web';
import { isDefined } from '../utils/nonNull';
interface InternalHttpResponseInit extends HttpResponseInit {
nativeResponse?: Response;
}
export class HttpResponse implements types.HttpResponse {
readonly cookies: types.Cookie[];
readonly enableContentNegotiation: boolean;
constructor(init?: InternalHttpResponseInit) {
init ??= {};
this.
if (init.nativeResponse) {
this.
} else {
const resInit: ResponseInit = { status: init.status, headers: init.headers };
if (isDefined(init.jsonBody)) {
// Response.json is not available in all versions, so we create it manually
const jsonBody = JSON.stringify(init.jsonBody);
const jsonHeaders = new Headers(resInit.headers);
if (!jsonHeaders.has('content-type')) {
jsonHeaders.set('content-type', 'application/json');
}
this.
} else {
// Cast to any to satisfy the native Response constructor
// Our HttpResponseBodyInit type is compatible with what Node.js accepts
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.
}
}
this.cookies = init.cookies ?? [];
this.enableContentNegotiation = !!init.enableContentNegotiation;
}
get status(): number {
return this.
}
get headers(): Headers {
return this.
}
get body(): ReadableStream<any> | null {
return this.
}
get bodyUsed(): boolean {
return this.
}
async arrayBuffer(): Promise<ArrayBuffer> {
return this.
}
async blob(): Promise<Blob> {
return this.
}
// eslint-disable-next-line deprecation/deprecation
async formData(): Promise<FormData> {
// eslint-disable-next-line deprecation/deprecation
return this.
}
async json(): Promise<unknown> {
return this.
}
async text(): Promise<string> {
return this.
}
clone(): HttpResponse {
const newInit = structuredClone(this.
newInit.nativeResponse = this.
return new HttpResponse(newInit);
}
}