Skip to content

Commit b93b393

Browse files
committed
add path names as parameters
1 parent 629645b commit b93b393

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/index.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable functional/no-expression-statements */
22
import type { Handler } from 'aws-lambda'
3-
import type { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda'
3+
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
44
import chromium from '@sparticuz/chromium'
55
import { Chromium } from './libs/chromium.js'
66
import type { PuppeteerLifeCycleEvent } from 'puppeteer-core'
@@ -30,23 +30,49 @@ const puppeteerLifeCycleEvents = [
3030
'networkidle2',
3131
] satisfies PuppeteerLifeCycleEvent[]
3232

33+
type APIGatewayEvent = Omit<APIGatewayProxyEvent, 'requestContext'> & {
34+
requestContext: APIGatewayProxyEvent['requestContext'] & {
35+
http?: { path?: string }
36+
}
37+
}
38+
39+
const U = undefined
40+
const EXP = /\/.+\/with\/([0-9]+)\/([0-9]+)\/(.+)/
41+
3342
export const handler: Handler = async ({
3443
queryStringParameters,
44+
requestContext,
3545
}: APIGatewayEvent): Promise<APIGatewayProxyResult> => {
3646
console.log('queryStringParameters', queryStringParameters)
47+
const path = requestContext.http?.path
48+
const pathParams = path
49+
? ((arg) =>
50+
arg
51+
? {
52+
w: arg[1],
53+
h: arg[2],
54+
src: decodeURIComponent(arg[3]).replace(
55+
/^(http)(s?):\/\/?/,
56+
'$1$2://',
57+
),
58+
}
59+
: U)(path.match(EXP))
60+
: U
61+
console.log('pathParams', pathParams)
3762
const params = { ...queryStringParameters }
38-
const { isDev, height, width, cacheControl, gotoThenWaitUntil } = {
63+
const { isDev, height, width, src, cacheControl, gotoThenWaitUntil } = {
3964
isDev: params['dev'] === 'true',
40-
height: params['h'],
41-
width: params['w'],
65+
height: pathParams?.h ?? params['h'],
66+
width: pathParams?.w ?? params['w'],
67+
src: pathParams?.src ?? params['src'],
4268
cacheControl: params['cache-control'],
4369
gotoThenWaitUntil: params['goto-then-waituntil'],
4470
}
4571
const options = isDev ? Localoptions : serverOptions
4672

4773
try {
4874
// Extract the "target" query parameter (the URL to capture)
49-
const targetUrl = params['src']
75+
const targetUrl = src
5076

5177
if (!targetUrl) {
5278
return { statusCode: 404, body: 'URL query parameter is required' }

0 commit comments

Comments
 (0)