Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing handler input type for Union #14

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const endpoint = factory.build({
})
});
```
- Supplying parameters to `EndpointsFactory::constructor()` is now prohibited. Please use `.addMiddleware()` and `.setResultHandler()` as the right way in order to achieve the correct input schema type in handlers.

### v0.6.1
- Nothing special. Just new logo and the dataflow diagram update.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"node-fetch": "^2.6.1",
"ts-jest": "^26.5.1",
"ts-node": "^9.1.1",
"tsd": "^0.15.1",
"typescript": "^4.1.5"
},
"repository": {
Expand Down
18 changes: 10 additions & 8 deletions src/endpoints-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ import {ResultHandler} from './result-handler';
/** mIN, mOUT - accumulated from all middlewares */
export class EndpointsFactory<mIN, mOUT> {
protected middlewares: MiddlewareDefinition<any, any, any>[] = [];
protected resultHandler: ResultHandler | null;
protected resultHandler: ResultHandler | null = null;

constructor(
middlewares: MiddlewareDefinition<any, any, any>[] = [],
resultHandler: ResultHandler | null = null
private static create<mIN, mOUT>(
middlewares: MiddlewareDefinition<any, any, any>[],
resultHandler: ResultHandler | null
) {
this.middlewares = middlewares;
this.resultHandler = resultHandler;
const factory = new EndpointsFactory<mIN, mOUT>();
factory.middlewares = middlewares;
factory.resultHandler = resultHandler;
return factory;
}

public setResultHandler(resultHandler: ResultHandler) {
return new EndpointsFactory<mIN, mOUT>(
return EndpointsFactory.create<mIN, mOUT>(
this.middlewares,
resultHandler
);
}

public addMiddleware<IN extends IOSchema, OUT extends FlatObject>(definition: MiddlewareDefinition<IN, mOUT, OUT>) {
return new EndpointsFactory<Merge<IN, mIN>, mOUT & OUT>(
return EndpointsFactory.create<Merge<IN, mIN>, mOUT & OUT>(
this.middlewares.concat(definition),
this.resultHandler
);
Expand Down
12 changes: 7 additions & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ import {LoggerConfig, loggerLevels} from './config-type';
import {MiddlewareDefinition} from './middleware';

export type FlatObject = Record<string, any>;
type EmptyFlatObject = {[K in never]: never};
// type EmptyFlatObject = {[K in never]: never};

type ObjectSchema = z.AnyZodObject;
type Extractable = 'shape' | '_unknownKeys' | '_catchall';
type Extractable = 'shape' | '_unknownKeys' | '_catchall' | '_output' | '_input';
type UnionSchema = z.ZodUnion<[ObjectSchema, ...ObjectSchema[]]>;
type IntersectionSchema = z.ZodIntersection<ObjectSchema, ObjectSchema>;

export type IOSchema = ObjectSchema | UnionSchema | IntersectionSchema;

type ArrayElement<T extends readonly unknown[]> = T extends readonly (infer K)[] ? K : never;

type UnionResult<T extends ObjectSchema[], F extends Extractable> = Partial<ArrayElement<T>[F]>;
type UnionResult<T extends ObjectSchema[], F extends Extractable> = ArrayElement<T>[F];
type IntersectionResult<T extends IntersectionSchema, F extends Extractable> =
T['_def']['left'][F] & T['_def']['right'][F];

type IOExtract<T extends IOSchema | any, F extends Extractable> =
T extends ObjectSchema ? T[F] :
T extends UnionSchema ? UnionResult<T['options'], F> :
T extends IntersectionSchema ? IntersectionResult<T, F> : EmptyFlatObject;
T extends IntersectionSchema ? IntersectionResult<T, F> : unknown; // : EmptyFlatObject

export type Merge<A extends IOSchema, B extends IOSchema | any> = z.ZodObject<
IOExtract<A, 'shape'> & IOExtract<B, 'shape'>,
IOExtract<A, '_unknownKeys'>,
IOExtract<A, '_catchall'>
IOExtract<A, '_catchall'>,
IOExtract<A, '_output'> & IOExtract<B, '_output'>,
IOExtract<A, '_input'> & IOExtract<B, '_input'>
>;

export function extractObjectSchema(subject: IOSchema): ObjectSchema {
Expand Down
Loading