Skip to content

Commit

Permalink
fix: use request type (QwikDev#2630)
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat authored Jan 12, 2023
1 parent 6976944 commit be115f6
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 330 deletions.
38 changes: 15 additions & 23 deletions packages/qwik-city/middleware/azure-swa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,14 @@ import type { AzureFunction, Context, HttpRequest } from '@azure/functions';
import type { RenderOptions } from '@builder.io/qwik';
import type { Render } from '@builder.io/qwik/server';
import qwikCityPlan from '@qwik-city-plan';
import { createHeaders, requestHandler } from '@builder.io/qwik-city/middleware/request-handler';
import { requestHandler } from '@builder.io/qwik-city/middleware/request-handler';
import type {
RequestContext,
ServerRenderOptions,
ServerRequestEvent,
} from '@builder.io/qwik-city/middleware/request-handler';

// @builder.io/qwik-city/middleware/azure-swa

function createQwikRequest(req: HttpRequest): RequestContext {
const url = req.headers['x-ms-original-url']!;

const headers = createHeaders();
for (const header in req.headers) {
headers.set(header, req.headers[header]);
}

return {
method: req.method || 'GET',
url: url,
headers,
formData: () => Promise.resolve(new URLSearchParams(req.params)),
json: req.body,
text: req.rawBody,
};
}

interface AzureResponse {
status: number;
headers: { [key: string]: any };
Expand All @@ -46,13 +27,24 @@ export function createQwikCity(opts: QwikCityAzureOptions): AzureFunction {
});
const decoder = new TextDecoder();
try {
const qwikRequest = createQwikRequest(req);
const getRequestBody = async function* () {
for await (const chunk of req as any) {
yield chunk;
}
};
const body = req.method === 'HEAD' || req.method === 'GET' ? undefined : getRequestBody();
const options = {
method: req.method,
headers: req.headers,
body: body as any,
duplex: 'half' as any,
};
const serverRequestEv: ServerRequestEvent<AzureResponse> = {
mode: 'server',
locale: undefined,
url: new URL(qwikRequest.url),
url: new URL(req.url),
platform: context,
request: qwikRequest,
request: new Request(req.url, options as any),
getWritableStream: (status, headers, _cookies) => {
res.status = status;
headers.forEach((value, key) => (res.headers[key] = value));
Expand Down
13 changes: 7 additions & 6 deletions packages/qwik-city/middleware/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ export async function fromNodeHttp(
};

const body = req.method === 'HEAD' || req.method === 'GET' ? undefined : getRequestBody();
const options = {
method: req.method,
headers: requestHeaders,
body: body as any,
duplex: 'half' as any,
};
const serverRequestEv: ServerRequestEvent<boolean> = {
mode,
url,
request: new (Request as any)(url.href, {
method: req.method,
headers: requestHeaders,
body,
duplex: 'half',
}) as any,
request: new Request(url.href, options as any),
getWritableStream: (status, headers, cookies) => {
res.statusCode = status;
headers.forEach((value, key) => res.setHeader(key, value));
Expand Down
15 changes: 1 addition & 14 deletions packages/qwik-city/middleware/request-handler/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ export interface CookieValue {
value: string;
}

// @alpha (undocumented)
export function createHeaders(): Headers;

// @alpha (undocumented)
export interface GetData {
// Warning: (ae-forgotten-export) The symbol "ServerLoader" needs to be exported by the entry point index.d.ts
Expand All @@ -84,16 +81,6 @@ export interface GetSyncData {
// @alpha (undocumented)
export const mergeHeadersCookies: (headers: Headers, cookies: Cookie) => Headers;

// @alpha (undocumented)
export interface RequestContext {
formData(): Promise<FormData>;
readonly headers: Headers;
json(): Promise<any>;
readonly method: string;
text(): Promise<string>;
readonly url: string;
}

// Warning: (ae-forgotten-export) The symbol "RequestEventCommon" needs to be exported by the entry point index.d.ts
//
// @alpha (undocumented)
Expand Down Expand Up @@ -136,7 +123,7 @@ export interface ServerRequestEvent<T = any> {
// (undocumented)
platform: any;
// (undocumented)
request: RequestContext;
request: Request;
// (undocumented)
url: URL;
}
Expand Down
127 changes: 0 additions & 127 deletions packages/qwik-city/middleware/request-handler/headers.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/qwik-city/middleware/request-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { createHeaders } from './headers';
export { getErrorHtml } from './error-handler';
export { mergeHeadersCookies } from './cookie';
export { requestHandler } from './request-handler';
Expand All @@ -9,7 +8,6 @@ export type {
CookieValue,
GetData,
GetSyncData,
RequestContext,
RequestEvent,
RequestHandler,
ServerRequestMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
ServerLoaderInternal,
} from '../../runtime/src/server-functions';
import { Cookie } from './cookie';
import { createHeaders } from './headers';
import { ErrorResponse } from './error-handler';
import { AbortMessage, RedirectMessage } from './redirect-handler';
import { encoder } from './resolve-request-handlers';
Expand All @@ -39,7 +38,7 @@ export function createRequestEvent(
const { request, platform } = serverRequestEv;

const cookie = new Cookie(request.headers.get('cookie'));
const headers = createHeaders();
const headers = new Headers();
const url = new URL(request.url);

let routeModuleIndex = -1;
Expand Down
68 changes: 0 additions & 68 deletions packages/qwik-city/middleware/request-handler/test-utils.ts

This file was deleted.

Loading

0 comments on commit be115f6

Please sign in to comment.