Skip to content

Commit

Permalink
Merge pull request #1170 from middyjs/feature/ts-import-fix
Browse files Browse the repository at this point in the history
Set explicit import type
  • Loading branch information
willfarrell authored Feb 6, 2024
2 parents 4a1c24d + e6271ea commit f7fcfda
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 42 deletions.
79 changes: 45 additions & 34 deletions packages/util/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import middy from '@middy/core'
import { Context as LambdaContext } from 'aws-lambda'
import { ArrayValues, Choose, DeepAwaited, IsUnknown, SanitizeKey, SanitizeKeys } from './type-utils'
import type {
ArrayValues,
Choose,
DeepAwaited,
IsUnknown,
SanitizeKey,
SanitizeKeys
} from './type-utils.d.ts'

interface Options<Client, ClientOptions> {
AwsClient?: new (...[config]: [any] | any) => Client
Expand All @@ -17,31 +24,33 @@ interface Options<Client, ClientOptions> {
declare class HttpError extends Error {
status: number
statusCode: number
expose: boolean
expose: boolean;
[key: string]: any
[key: number]: any
}

declare function createPrefetchClient<Client, ClientOptions> (
declare function createPrefetchClient<Client, ClientOptions>(
options: Options<Client, ClientOptions>
): Client

declare function createClient<Client, ClientOptions> (
declare function createClient<Client, ClientOptions>(
options: Options<Client, ClientOptions>,
request: middy.Request
): Promise<Client>

declare function canPrefetch<Client, ClientOptions> (
declare function canPrefetch<Client, ClientOptions>(
options: Options<Client, ClientOptions>
): boolean

type InternalOutput<TVariables> = TVariables extends string[] ? { [key in TVariables[number]]: unknown } : never
type InternalOutput<TVariables> = TVariables extends string[]
? { [key in TVariables[number]]: unknown }
: never

// get an empty object if false is passed
declare function getInternal<
TContext extends LambdaContext,
TInternal extends Record<string, unknown>
> (
>(
variables: false,
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
): Promise<{}>
Expand All @@ -50,7 +59,7 @@ declare function getInternal<
declare function getInternal<
TContext extends LambdaContext,
TInternal extends Record<string, unknown>
> (
>(
variables: true,
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
): Promise<DeepAwaited<TInternal>>
Expand All @@ -60,77 +69,79 @@ declare function getInternal<
TContext extends LambdaContext,
TInternal extends Record<string, unknown>,
TVars extends keyof TInternal | string
> (
>(
variables: TVars,
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
): TVars extends keyof TInternal
? Promise<DeepAwaited<{ [_ in SanitizeKey<TVars>]: TInternal[TVars] }>>
: TVars extends string
? IsUnknown<Choose<DeepAwaited<TInternal>, TVars>> extends true
? unknown // could not find the path
: Promise<{ [_ in SanitizeKey<TVars>]: Choose<DeepAwaited<TInternal>, TVars> }>
: unknown // path is not a string or a keyof TInternal
? IsUnknown<Choose<DeepAwaited<TInternal>, TVars>> extends true
? unknown // could not find the path
: Promise<{
[_ in SanitizeKey<TVars>]: Choose<DeepAwaited<TInternal>, TVars>
}>
: unknown // path is not a string or a keyof TInternal

// get multiple values
declare function getInternal<
TContext extends LambdaContext,
TInternal extends Record<string, unknown>,
TVars extends Array<keyof TInternal | string>
> (
>(
variables: TVars,
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
): Promise<SanitizeKeys<{
[TVar in ArrayValues<TVars>]:
TVar extends keyof TInternal
? DeepAwaited<TInternal[TVar]>
: TVar extends string
): Promise<
SanitizeKeys<{
[TVar in ArrayValues<TVars>]: TVar extends keyof TInternal
? DeepAwaited<TInternal[TVar]>
: TVar extends string
? Choose<DeepAwaited<TInternal>, TVar>
: unknown // path is not a string or a keyof TInternal
}>>
}>
>

// remap object
declare function getInternal<
TContext extends LambdaContext,
TInternal extends Record<string, unknown>,
TMap extends Record<string, keyof TInternal | string>
> (
>(
variables: TMap,
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>
): Promise<{
[P in keyof TMap]:
TMap[P] extends keyof TInternal
[P in keyof TMap]: TMap[P] extends keyof TInternal
? DeepAwaited<TInternal[TMap[P]]>
: TMap[P] extends string
? Choose<DeepAwaited<TInternal>, TMap[P]>
: unknown // path is not a string or a keyof TInternal
? Choose<DeepAwaited<TInternal>, TMap[P]>
: unknown // path is not a string or a keyof TInternal
}>

declare function sanitizeKey<T extends string> (key: T): SanitizeKey<T>
declare function sanitizeKey<T extends string>(key: T): SanitizeKey<T>

declare function processCache<Client, ClientOptions> (
declare function processCache<Client, ClientOptions>(
options: Options<Client, ClientOptions>,
fetch: (request: middy.Request, cachedValues: any) => any,
request?: middy.Request
): { value: any, expiry: number }
): { value: any; expiry: number }

declare function getCache (keys: string): any
declare function getCache(keys: string): any

declare function clearCache (keys?: string | string[] | null): void
declare function clearCache(keys?: string | string[] | null): void

declare function jsonSafeParse (
declare function jsonSafeParse(
string: string,
reviver?: (key: string, value: any) => any
): any

declare function normalizeHttpResponse (
declare function normalizeHttpResponse(
request: any,
fallbackResponse?: any
): any

declare function createError (
declare function createError(
code: number,
message: string,
properties?: Record<string, any>
): HttpError

declare function modifyCache (cacheKey: string, value: any): void
declare function modifyCache(cacheKey: string, value: any): void
27 changes: 20 additions & 7 deletions packages/util/type-utils.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectType } from 'tsd'
import {
import type {
SanitizeKeyPrefixLeadingNumber,
SanitizeKeyRemoveDisallowedChar,
RemoveAllLeadingUnderscore,
Expand All @@ -10,7 +10,7 @@ import {
IsUnknown,
ArrayValues,
SanitizeKeys
} from './type-utils'
} from './type-utils.d.ts'

// SanitizeKeyPrefixLeadingNumber
// adds a leading _ if it starts with a number
Expand All @@ -27,7 +27,9 @@ expectType<SanitizeKeyPrefixLeadingNumber<'-abcd'>>('-abcd')

// SanitizeKeyRemoveDisallowedChar
// removes all disallowed chars and replaces them with _
expectType<SanitizeKeyRemoveDisallowedChar<'1234!@#AA$%^&*()BBB_+{}|:"<>?~CC`-=[D]E\\;,./F'>>('1234___AA_______BBB___________CC____D_E_____F')
expectType<
SanitizeKeyRemoveDisallowedChar<'1234!@#AA$%^&*()BBB_+{}|:"<>?~CC`-=[D]E\\;,./F'>
>('1234___AA_______BBB___________CC____D_E_____F')

// RemoveAllLeadingUnderscore
// removes all leading _
Expand Down Expand Up @@ -57,11 +59,20 @@ expectType<SanitizeKey<'0key.0key'>>('_0key_0key')
// SanitizeKeys
// sanitizes all keys in an object
expectType<SanitizeKeys<{ '0key': 0 }>>({ _0key: 0 })
expectType<SanitizeKeys<{ 'api//secret-key0.pem': 0 }>>({ api_secret_key0_pem: 0 })
expectType<SanitizeKeys<{ '0key': 0, 'api//secret-key0.pem': 0 }>>({ _0key: 0, api_secret_key0_pem: 0 })
expectType<SanitizeKeys<{ 'api//secret-key0.pem': 0 }>>({
api_secret_key0_pem: 0
})
expectType<SanitizeKeys<{ '0key': 0; 'api//secret-key0.pem': 0 }>>({
_0key: 0,
api_secret_key0_pem: 0
})

// DeepAwaited
expectType<DeepAwaited<{ level1: { level2: Promise<Promise<{ innerPromise: Promise<22> }>> } }>>({
expectType<
DeepAwaited<{
level1: { level2: Promise<Promise<{ innerPromise: Promise<22> }>> }
}>
>({
level1: {
level2: {
innerPromise: Promise.resolve(22) // Note: getInternal does not recurse into resolved promises
Expand All @@ -88,7 +99,9 @@ expectType<Choose<TestChoose, 'number'>>(1)
expectType<Choose<TestChoose, 'object.key'>>('value')
expectType<Choose<TestChoose, 'promise'>>(Promise.resolve('string'))
expectType<Choose<DeepAwaited<TestChoose>, 'promise'>>('string')
expectType<Choose<TestChoose, 'promiseObject'>>(Promise.resolve({ key: 'value' }))
expectType<Choose<TestChoose, 'promiseObject'>>(
Promise.resolve({ key: 'value' })
)
expectType<Choose<DeepAwaited<TestChoose>, 'promiseObject'>>({ key: 'value' })

// IsUnknown
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/transpile.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectType } from 'tsd'
import { transpileSchema, transpileLocale } from './transpile'
import type { transpileSchema, transpileLocale } from './transpile.t.ds'

const schema = transpileSchema({ type: 'object' }, {})
expectType<any>(schema)
Expand Down

0 comments on commit f7fcfda

Please sign in to comment.