Skip to content

Commit

Permalink
fix: yup type inference bug
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
refactorthis committed Sep 5, 2024
1 parent 1205cfe commit ca9add2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 56 deletions.
12 changes: 6 additions & 6 deletions examples/sst-v2/packages/functions/src/api-yup/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ export const GetTodoPath = object({
})

export const ListQuery = object({
skip: number().optional(),
take: number().optional(),
skip: number(),
take: number(),
})

export const CreateTodoRequest = object({
id: string(),
title: string(),
description: string(),
due: date(),
id: string().required(),
title: string().required(),
description: string().required(),
due: date().required(),
})

export const TodoResponse = object({
Expand Down
2 changes: 2 additions & 0 deletions examples/sst-v2/packages/functions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"module": "esnext",
"moduleResolution": "node",
"baseUrl": ".",
"strictNullChecks": true,
"strictFunctionTypes": false,
"allowSyntheticDefaultImports": true,
"paths": {
"@sst-v2/core/*": ["../core/src/*"],
Expand Down
49 changes: 1 addition & 48 deletions package/src/core/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* parsers.ts
*
* Schema parsing / validation types
* We use duck typing here, so that we don't need to pull in the package deps.
*/
Expand All @@ -11,52 +9,7 @@ export type ZodSchemaLike<TInput> = {
}

export type YupSchemaLike<TInput> = {
validate(value: TInput, options?: any): Promise<any>
validate(value: unknown, options?: any): Promise<TInput>
}

export type Schema<T> = ZodSchemaLike<T> | YupSchemaLike<T>

/**
* Api parsing options
*/
export interface ApiParser<TResponse, TRequest, TPath, TQuery> {
/**
* Request body parser
*/
request?: Schema<TRequest>

/**
* Response parser.
*
* If you would like to skip runtime validation of this schema, set the validateResponses property to false.
*
* @example
* ```typescript
* parser: {
* response: z.object({})
* }
* ```
*/
response?: Schema<TResponse>

/**
* URI path parser
*/
path?: Schema<TPath>

/**
* URI querystring parser
*/
query?: Schema<TQuery>

/**
* Response validation setting
*
* - 'never' - do not perform response validation
* - 'warn' - log warning and proceed
* - 'error' - raise 500 error
*
* @default 'error'
*/
validateResponses?: 'never' | 'warn' | 'error'
}
49 changes: 47 additions & 2 deletions package/src/integrations/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Options as CorsOptions } from '@middy/http-cors'
import { Context, FuncyOptions } from 'package/src/core'
import { Context, FuncyOptions } from '@core'
import { APIGatewayProxyStructuredResultV2 } from 'aws-lambda'
// WORKAROUND: Found a strange behaviour where linking via alias will break implicit typing..
import { ApiParser } from '../../core/parsers'
import { Schema } from '../../core/parsers'

//export type APIGatewayResult = APIGatewayProxyStructuredResultV2 | APIGatewayProxyResult

Expand Down Expand Up @@ -125,6 +125,51 @@ export interface FuncyApiOptions<
}
}

/**
* Api parsing options
*/
export interface ApiParser<TResponse, TRequest, TPath, TQuery> {
/**
* Request body parser
*/
request?: Schema<TRequest>

/**
* Response parser.
*
* If you would like to skip runtime validation of this schema, set the validateResponses property to false.
*
* @example
* ```typescript
* parser: {
* response: z.object({})
* }
* ```
*/
response?: Schema<TResponse>

/**
* URI path parser
*/
path?: Schema<TPath>

/**
* URI querystring parser
*/
query?: Schema<TQuery>

/**
* Response validation setting
*
* - 'never' - do not perform response validation
* - 'warn' - log warning and proceed
* - 'error' - raise 500 error
*
* @default 'error'
*/
validateResponses?: 'never' | 'warn' | 'error'
}

// NOTE: Not exported from middy packages
interface SecurityOptions {
dnsPrefetchControl?: {
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"baseUrl": ".",
"allowSyntheticDefaultImports": true,
"declaration": true,
"strictNullChecks": true,
"strictFunctionTypes": false,
"outDir": "./dist",
"paths": {
"@core": ["package/src/core"],
Expand Down

0 comments on commit ca9add2

Please sign in to comment.