Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const qs = stringifyQsParams(qsParams);
const url = `${this.url}${this.modifyPath(path)}${qs}`;
const response = await fetch(
url,
// @ts-ignore incompatible fetch type and EncodedHttpRequest
{ method, headers, body }
).catch((e: Error) => {
throw createLocalError(e, "NetworkFailed");
});
const encodedResponse = {
body: await response.json(),
statusCode: response.status,
headers: response.headers
}; // FIXME: headers from polyfilled fetch don't implement Headers API.
// @ts-ignore http-client will not receive any types beyond this point
return decodeResponse(encodedResponse);
}
>(
reqData: RequestDataWithTypeMapForResponse
): Promise | ErrorResponseData> {
// TODO: use HandlerRequest Type instead of Promise
const { method, headers, path, qsParams, body } = encodeRequest(reqData);
const qs = stringifyQsParams(qsParams);
const url = `${this.url}${this.modifyPath(path)}${qs}`;
const response = await fetch(
url,
// @ts-ignore incompatible fetch type and EncodedHttpRequest
{ method, headers, body }
).catch((e: Error) => {
throw createLocalError(e, "NetworkFailed");
});
const encodedResponse = {
body: await response.json(),
statusCode: response.status,
headers: response.headers
}; // FIXME: headers from polyfilled fetch don't implement Headers API.
// @ts-ignore http-client will not receive any types beyond this point
return decodeResponse(encodedResponse);
return async (req: Request, res: Response, next: NextFunction) => {
const { path, method, query, headers, body } = req;
if (!pathRegex.test(path)) {
return next();
}
if (
method !== "GET" &&
method !== "POST" &&
method !== "PUT" &&
method !== "DELETE"
) {
return next();
}
const serverLogic = new ServerLogic(serverParams);
const encodedHttpRequest: EncodedHttpRequest = {
method,
headers: headers as EncodedHttpRequest["headers"],
path,
qsParams: query
};
if (!body) {
encodedHttpRequest.body = await getRawBody(req, true);
} else if (typeof body === "object") {
encodedHttpRequest.parsedBody = body;
} else {
encodedHttpRequest.body = body;
}
const response = await serverLogic.handleRequest(encodedHttpRequest);
res
return async (
event: LambdaEvent,
context: LambdaContext,
cb: LambdaCallback
): Promise => {
/**
* Universal server logic.
* Offers the flow: EncodedHttpRequest => EncodedHttpResponse
*/
const logic = new ServerLogic(params);
const encodedHttpRequest = {
method: event.httpMethod,
path: event.path,
body: event.body,
headers: event.headers,
qsParams: event.queryStringParameters
};
const encodedHttpResponse = await logic.handleRequest(encodedHttpRequest);
cb(null, encodedHttpResponse);
};
};
constructor(server: http.Server | https.Server, params: GeneralServerParams) {
this.server = server;
this.logic = new ServerLogic(params);
}
/**
const encodedHttpRequest: EncodedHttpRequest = {
method,
headers: headers as EncodedHttpRequest["headers"],
path,
qsParams: query
};
if (!body) {
encodedHttpRequest.body = await getRawBody(req, true);
} else if (typeof body === "object") {
encodedHttpRequest.parsedBody = body;
} else {
encodedHttpRequest.body = body;
}
let responseData: GeneralResponseData;
try {
const requestData = decodeRequest(encodedHttpRequest);
responseData = await restApiHandler.handleRequestData(requestData);
} catch (err) {
responseData = { type: "error", payload: createServerError(err) };
}
res.status(getStatusCode(responseData)).json(responseData);
};
}
};
if (!body) {
encodedHttpRequest.body = await getRawBody(req, true);
} else if (typeof body === "object") {
encodedHttpRequest.parsedBody = body;
} else {
encodedHttpRequest.body = body;
}
let responseData: GeneralResponseData;
try {
const requestData = decodeRequest(encodedHttpRequest);
responseData = await restApiHandler.handleRequestData(requestData);
} catch (err) {
responseData = { type: "error", payload: createServerError(err) };
}
res.status(getStatusCode(responseData)).json(responseData);
};
}