blomqma / next-rest-framework

Type-safe, self-documenting APIs for Next.js
https://next-rest-framework.vercel.app
Other
134 stars 17 forks source link

suggested way to access typed query params #171

Open ekrata-main opened 2 months ago

ekrata-main commented 2 months ago
  createParam: routeOperation({
    method: 'PUT',
    openApiOperation: {
      description: 'Creates a parameter.',
      summary: 'Creates a parameter'
    }
  })
    .input({
      query: createParamQuery
    })
    .outputs([
      {
        name: 'StorageCreateParamResponse',
        status: 200,
        contentType: 'application/json',
        body: z.object({ secret: z.string() })
      },
      {
        name: 'StorageCreateParamError',
        status: 500,
        contentType: 'application/json',
        body: z.object({})
      }
    ])
    .handler(async (request) => {
      try {
        const { nextUrl } = request;
        const key = nextUrl.searchParams.get('key') ?? '';
        const value = nextUrl.searchParams.get('value') ?? '';
        await createParam(`/params/${key}`, value);
        return TypedNextResponse.json({}, { status: 200 });
      } catch (err) {
        storageLogger.error((err as Error).message, err as Error);
        return TypedNextResponse.json({}, { status: 500, statusText: `Internal Server Error` });
      }
    })

searchParams.get() is typed, but getting searchParams.get() will always add 'undefined' to a query param type, even if it is not undefined(asserted by zod). Any advice?? am I using the wrong object to access the query key? couldn't find anything in the docs