Skip to content

Commit

Permalink
fix(typescript): Make additional types generic to work with extended …
Browse files Browse the repository at this point in the history
…types (#2625)
  • Loading branch information
daffl authored May 18, 2022
1 parent afe9a3b commit 269fdec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/mongodb/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface MongoDBAdapterParams<Q = AdapterQuery> extends AdapterParams<Q,
}

// Create the service.
export class MongoDbAdapter<T, D = Partial<T>, P extends MongoDBAdapterParams = MongoDBAdapterParams>
export class MongoDbAdapter<T, D = Partial<T>, P extends MongoDBAdapterParams<any> = MongoDBAdapterParams>
extends AdapterBase<T, D, P, MongoDBAdapterOptions> {
constructor (options: MongoDBAdapterOptions) {
if (!options) {
Expand Down
30 changes: 15 additions & 15 deletions packages/schema/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BadRequest } from '../../errors/lib';
import { Resolver, ResolverStatus } from './resolver';
import { Schema } from './schema';

const getContext = (context: HookContext) => {
const getContext = <H extends HookContext> (context: H) => {
return {
...context,
params: {
Expand All @@ -13,11 +13,11 @@ const getContext = (context: HookContext) => {
}
}

const runResolvers = async <T> (
resolvers: Resolver<T, HookContext>[],
const runResolvers = async <T, H extends HookContext> (
resolvers: Resolver<T, H>[],
data: any,
ctx: HookContext,
status?: Partial<ResolverStatus<T, HookContext>>
ctx: H,
status?: Partial<ResolverStatus<T, H>>
) => {
let current: any = data;

Expand All @@ -28,8 +28,8 @@ const runResolvers = async <T> (
return current as T;
}

export const resolveQuery = <T> (...resolvers: Resolver<T, HookContext>[]) =>
async (context: HookContext, next?: NextFunction) => {
export const resolveQuery = <T, H extends HookContext> (...resolvers: Resolver<T, H>[]) =>
async (context: H, next?: NextFunction) => {
const ctx = getContext(context);
const data = context?.params?.query || {};
const query = await runResolvers(resolvers, data, ctx);
Expand All @@ -44,8 +44,8 @@ export const resolveQuery = <T> (...resolvers: Resolver<T, HookContext>[]) =>
}
};

export const resolveData = <T> (...resolvers: Resolver<T, HookContext>[]) =>
async (context: HookContext, next?: NextFunction) => {
export const resolveData = <T, H extends HookContext> (...resolvers: Resolver<T, H>[]) =>
async (context: H, next?: NextFunction) => {
const ctx = getContext(context);
const data = context.data;
const status = {
Expand All @@ -65,8 +65,8 @@ export const resolveData = <T> (...resolvers: Resolver<T, HookContext>[]) =>
}
};

export const resolveResult = <T> (...resolvers: Resolver<T, HookContext>[]) =>
async (context: HookContext, next?: NextFunction) => {
export const resolveResult = <T, H extends HookContext> (...resolvers: Resolver<T, H>[]) =>
async (context: H, next?: NextFunction) => {
if (typeof next === 'function') {
const { $resolve: properties, ...query } = context.params?.query || {};
const resolve = {
Expand Down Expand Up @@ -101,8 +101,8 @@ export const resolveResult = <T> (...resolvers: Resolver<T, HookContext>[]) =>
}
};

export const validateQuery = (schema: Schema<any>) =>
async (context: HookContext, next?: NextFunction) => {
export const validateQuery = <H extends HookContext> (schema: Schema<any>) =>
async (context: H, next?: NextFunction) => {
const data = context?.params?.query || {};

try {
Expand All @@ -121,8 +121,8 @@ export const validateQuery = (schema: Schema<any>) =>
}
};

export const validateData = (schema: Schema<any>) =>
async (context: HookContext, next?: NextFunction) => {
export const validateData = <H extends HookContext> (schema: Schema<any>) =>
async (context: H, next?: NextFunction) => {
const data = context.data;

try {
Expand Down

0 comments on commit 269fdec

Please # to comment.