@api-ts/io-ts-http
Advanced tools
Comparing version 1.0.0 to 2.0.0
import * as t from 'io-ts'; | ||
import { Flattened, NestedType, NestedOutputType, NestedProps, OptionalizedC, Simplify } from './utils'; | ||
export declare const optional: <C extends t.Mixed>(subCodec: C) => t.UnionC<[C, t.UndefinedC]>; | ||
export declare const optionalized: <P extends t.Props>(props: P) => OptionalizedC<P>; | ||
export declare const optional: <C extends t.Mixed>(codec: C) => t.UnionC<[C, t.UndefinedC]>; | ||
export declare const optionalized: <P extends t.Props>(props: P, name?: string) => OptionalizedC<P>; | ||
export declare const flattened: <Props extends NestedProps>(name: string, props: Props) => t.Type<Simplify<(NestedType<Props>[keyof Props] extends infer T ? T extends NestedType<Props>[keyof Props] ? T extends any ? (x: T) => any : never : never : never) extends (x: infer R) => any ? R : never>, Simplify<NestedOutputType<Props>>, unknown>; | ||
//# sourceMappingURL=combinators.d.ts.map |
@@ -31,13 +31,31 @@ "use strict"; | ||
const t = __importStar(require("io-ts")); | ||
const optional = (subCodec) => t.union([subCodec, t.undefined]); | ||
const partialWithoutUndefined = (props, name) => { | ||
const partialCodec = t.partial(props, name); | ||
return new t.PartialType(partialCodec.name, (i) => partialCodec.is(i) && !Object.values(i).includes(void 0), (i, ctx) => { | ||
return (0, function_1.pipe)(partialCodec.validate(i, ctx), E.map((result) => { | ||
for (const key of Object.keys(result)) { | ||
if (result[key] === void 0) { | ||
delete result[key]; | ||
} | ||
} | ||
return result; | ||
})); | ||
}, (a) => { | ||
const result = partialCodec.encode(a); | ||
for (const key of Object.keys(result)) { | ||
if (result[key] === void 0) { | ||
delete result[key]; | ||
} | ||
} | ||
return result; | ||
}, props); | ||
}; | ||
const optional = (codec) => t.union([codec, t.undefined]); | ||
exports.optional = optional; | ||
const optionalized = (props) => { | ||
const optionalized = (props, name) => { | ||
const requiredProps = {}; | ||
const optionalProps = {}; | ||
for (const key in props) { | ||
if (!props.hasOwnProperty(key)) { | ||
continue; | ||
} | ||
for (const key of Object.keys(props)) { | ||
const codec = props[key]; | ||
const isOptional = codec.is(undefined); | ||
const isOptional = codec.is(void 0); | ||
if (isOptional) { | ||
@@ -51,4 +69,4 @@ optionalProps[key] = codec; | ||
return t.intersection([ | ||
t.type(requiredProps), | ||
t.partial(optionalProps), | ||
t.type(requiredProps, name ? `required_${name}` : undefined), | ||
partialWithoutUndefined(optionalProps, name ? `optional_${name}` : undefined), | ||
]); | ||
@@ -55,0 +73,0 @@ }; |
@@ -9,3 +9,3 @@ import * as t from 'io-ts'; | ||
}>; | ||
export declare type HttpRequestCodec<T> = t.Type<T, t.TypeOf<typeof GenericHttpRequest>, unknown>; | ||
export declare type HttpRequestCodec<T> = t.Type<T, t.OutputOf<typeof GenericHttpRequest>, unknown>; | ||
export declare type HttpRequestCombinatorProps = { | ||
@@ -12,0 +12,0 @@ params?: NonNullable<t.Props>; |
import type * as t from 'io-ts'; | ||
declare type Defined<T> = T extends undefined ? never : T; | ||
export declare type DefinedValues<T> = { | ||
[K in keyof T]: Defined<T[K]>; | ||
}; | ||
declare type DefinedProps<Props extends t.Props> = { | ||
[K in keyof Props]: Props[K] extends t.Type<infer A, infer O, infer I> ? t.Type<Defined<A>, O, I> : never; | ||
}; | ||
declare type PossiblyUndefinedKeys<T> = { | ||
@@ -8,4 +15,4 @@ [K in keyof T]: undefined extends T[K] ? K : never; | ||
}[keyof T]; | ||
declare type Optionalized<T> = Simplify<Omit<T, PossiblyUndefinedKeys<T>> & Partial<Pick<T, PossiblyUndefinedKeys<T>>>>; | ||
export declare type OptionalProps<Props extends t.Props> = Pick<Props, PossiblyUndefinedProps<Props>>; | ||
declare type Optionalized<T> = Simplify<Omit<T, PossiblyUndefinedKeys<T>> & Partial<DefinedValues<Pick<T, PossiblyUndefinedKeys<T>>>>>; | ||
export declare type OptionalProps<Props extends t.Props> = DefinedProps<Pick<Props, PossiblyUndefinedProps<Props>>>; | ||
export declare type RequiredProps<Props extends t.Props> = Omit<Props, PossiblyUndefinedProps<Props>>; | ||
@@ -12,0 +19,0 @@ export declare type OptionalizedC<Props extends t.Props> = t.IntersectionC<[ |
{ | ||
"name": "@api-ts/io-ts-http", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Types for (de)serializing HTTP requests from both the client and server side", | ||
@@ -23,12 +23,12 @@ "author": "Patrick McLaughlin <patrickmclaughlin@bitgo.com>", | ||
"@api-ts/response": "1.0.0", | ||
"fp-ts": "2.12.3", | ||
"fp-ts": "2.13.1", | ||
"io-ts": "2.1.3", | ||
"io-ts-types": "0.5.16" | ||
"io-ts-types": "0.5.19" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "4.3.3", | ||
"@types/mocha": "9.1.1", | ||
"c8": "7.11.2", | ||
"chai": "4.3.6", | ||
"mocha": "10.0.0", | ||
"@types/chai": "4.3.4", | ||
"@types/mocha": "10.0.0", | ||
"c8": "7.12.0", | ||
"chai": "4.3.7", | ||
"mocha": "10.1.0", | ||
"ts-node": "10.9.1", | ||
@@ -35,0 +35,0 @@ "typescript": "4.7.4" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41102
386
+ Addedfp-ts@2.13.1(transitive)
+ Addedio-ts-types@0.5.19(transitive)
- Removedfp-ts@2.12.3(transitive)
- Removedio-ts-types@0.5.16(transitive)
Updatedfp-ts@2.13.1
Updatedio-ts-types@0.5.19