Socket
Socket
Sign inDemoInstall

18h

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

18h - npm Package Compare versions

Comparing version 2.0.7 to 2.1.0

26

dist/@types/http-method.d.ts

@@ -8,10 +8,12 @@ import { Context, Middleware } from "koa";

};
export declare type ExtendedContext<RequestBody extends ObjectShape | null = null, QueryParams = Record<string, string | undefined>> = Context & {
request: Context["request"] & {
body: RequestBody extends ObjectShape ? RecursivePartial<ObjectSchema<RequestBody>["__outputType"]> : null;
};
params: QueryParams;
};
export declare type HTTPMethodRules<ResponseBody = object, RequestBody extends ObjectShape | null = null, QueryParams = Record<string, string | undefined>> = {
handler(context?: ExtendedContext<RequestBody, QueryParams>): Promise<RequestHandlerResult<ResponseBody>>;
interface OverrideBody<T> extends Exclude<Context["request"], "body"> {
body: T;
}
interface OverrideRequest<T, RouteParams> extends Exclude<Context, "body"> {
request: OverrideBody<T>;
params: RouteParams;
}
export declare type ExtendedContext<RequestBody extends ObjectShape | null = null, RouteParams = Record<string, string | undefined>> = OverrideRequest<RequestBody extends ObjectShape ? RecursivePartial<ObjectSchema<RequestBody>["__outputType"]> : {}, RouteParams>;
export declare type RouteHandlerRules<ResponseBody = unknown, RequestBody extends ObjectShape | null = null, RouteParams = Record<string, string | undefined>> = {
handler(context?: ExtendedContext<RequestBody, RouteParams>): Promise<RequestHandlerResult<ResponseBody>>;
middleware?: {

@@ -27,9 +29,3 @@ pre?: Middleware[];

export declare type MethodName = "any" | "delete" | "get" | "delete" | "patch" | "post" | "put";
export declare type HTTPMethod<Method extends MethodName, ResponseBody, RequestBody extends ObjectShape | null = null, QueryParams = Record<string, string | undefined>> = Record<Method, HTTPMethodRules<ResponseBody, RequestBody, QueryParams>>;
export declare type ANY<ResponseBody = null, RequestBody extends ObjectShape | null = null> = HTTPMethod<"any", ResponseBody, RequestBody>;
export declare type DELETE<ResponseBody = null, RequestBody extends ObjectShape | null = null> = HTTPMethod<"delete", ResponseBody, RequestBody>;
export declare type GET<ResponseBody = null, RequestBody extends ObjectShape | null = null> = HTTPMethod<"get", ResponseBody, RequestBody>;
export declare type PATCH<ResponseBody = null, RequestBody extends ObjectShape | null = null> = HTTPMethod<"patch", ResponseBody, RequestBody>;
export declare type POST<ResponseBody = null, RequestBody extends ObjectShape | null = null> = HTTPMethod<"post", ResponseBody, RequestBody>;
export declare type PUT<ResponseBody = null, RequestBody extends ObjectShape | null = null> = HTTPMethod<"put", ResponseBody, RequestBody>;
export declare type MethodController<ResponseBody = null, RequestBody extends ObjectShape | null = null, RouteParams = Record<string, string | undefined>> = RouteHandlerRules<ResponseBody, RequestBody, RouteParams>;
export {};

@@ -1,5 +0,8 @@

export interface RequestHandlerResult<ResponseBody> {
export declare type RequestHandlerResult<ResponseBody> = {
headers?: Record<string, string | number | boolean>;
code?: number;
} & (ResponseBody extends null ? {
body?: never;
} : {
body: ResponseBody;
}
});

@@ -1,7 +0,8 @@

import { HTTPMethod } from "../@types/http-method";
import { MethodController, MethodName } from "../@types/http-method";
import { handleRoute } from "../util/routing";
export declare type ParsedRouteController = ReturnType<typeof handleRoute>;
declare type UnionIntersect<U, QueryParams> = (U extends object ? (k: U) => void : object) extends (k: infer I) => void ? I extends HTTPMethod<infer A, infer B, infer C> ? HTTPMethod<A, B, C, QueryParams> : object : object;
declare type Merge<A extends object[], QueryParams> = UnionIntersect<A[number], QueryParams>;
export declare type RouteController<AllowedMethods extends object[] = [], QueryParams extends Record<string, string | undefined> = {}> = Merge<AllowedMethods, QueryParams>;
export {};
export declare type RouteController<AllowedMethods extends {
[K in MethodName]?: object;
} | null = null, RouteParams extends Record<string, string | undefined> = {}> = AllowedMethods extends null ? Record<string, never> : {
[K in keyof AllowedMethods]: AllowedMethods[K] extends MethodController<infer A, infer B> ? MethodController<A, B, RouteParams> : unknown;
};

@@ -5,3 +5,3 @@ import { RouteController } from "../@types/route-controller";

method: MethodName;
internalHandler: (context: ExtendedContext) => Promise<void>;
internalHandler: (context: ExtendedContext<null, Record<string, string | undefined>>) => Promise<void>;
middleware: {

@@ -8,0 +8,0 @@ pre?: import("koa").Middleware<import("koa").DefaultState, import("koa").DefaultContext, any>[] | undefined;

@@ -17,5 +17,5 @@ "use strict";

const internalHandler = (context) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
var _a, _b;
const { body = null, code = 200, headers = {}, } = yield rule.handler(context);
const errors = yield ((_a = rule.validation) === null || _a === void 0 ? void 0 : _a.validate(context.request.body).catch(({ errors }) => errors));
const errors = yield ((_a = rule.validation) === null || _a === void 0 ? void 0 : _a.validate((_b = context.request) === null || _b === void 0 ? void 0 : _b.body).catch(({ errors }) => errors));
if (errors === null || errors === void 0 ? void 0 : errors.length) {

@@ -22,0 +22,0 @@ context.status = 400;

@@ -7,24 +7,31 @@ import { Context, Middleware } from "koa";

type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
[P in keyof T]?: RecursivePartial<T[P]>;
};
interface OverrideBody<T> extends Exclude<Context["request"], "body"> {
body: T;
}
interface OverrideRequest<T, RouteParams> extends Exclude<Context, "body"> {
request: OverrideBody<T>;
params: RouteParams;
}
export type ExtendedContext<
RequestBody extends ObjectShape | null = null,
QueryParams = Record<string, string | undefined>
> = Context & {
request: Context["request"] & {
body: RequestBody extends ObjectShape
? RecursivePartial<ObjectSchema<RequestBody>["__outputType"]>
: null;
};
params: QueryParams;
};
RouteParams = Record<string, string | undefined>
> = OverrideRequest<
RequestBody extends ObjectShape
? RecursivePartial<ObjectSchema<RequestBody>["__outputType"]>
: {},
RouteParams
>;
export type HTTPMethodRules<
ResponseBody = object,
export type RouteHandlerRules<
ResponseBody = unknown,
RequestBody extends ObjectShape | null = null,
QueryParams = Record<string, string | undefined>
RouteParams = Record<string, string | undefined>
> = {
handler(
context?: ExtendedContext<RequestBody, QueryParams>
context?: ExtendedContext<RequestBody, RouteParams>
): Promise<RequestHandlerResult<ResponseBody>>;

@@ -54,37 +61,6 @@ middleware?: {

export type HTTPMethod<
Method extends MethodName,
ResponseBody,
export type MethodController<
ResponseBody = null,
RequestBody extends ObjectShape | null = null,
QueryParams = Record<string, string | undefined>
> = Record<Method, HTTPMethodRules<ResponseBody, RequestBody, QueryParams>>;
export type ANY<
ResponseBody = null,
RequestBody extends ObjectShape | null = null
> = HTTPMethod<"any", ResponseBody, RequestBody>;
export type DELETE<
ResponseBody = null,
RequestBody extends ObjectShape | null = null
> = HTTPMethod<"delete", ResponseBody, RequestBody>;
export type GET<
ResponseBody = null,
RequestBody extends ObjectShape | null = null
> = HTTPMethod<"get", ResponseBody, RequestBody>;
export type PATCH<
ResponseBody = null,
RequestBody extends ObjectShape | null = null
> = HTTPMethod<"patch", ResponseBody, RequestBody>;
export type POST<
ResponseBody = null,
RequestBody extends ObjectShape | null = null
> = HTTPMethod<"post", ResponseBody, RequestBody>;
export type PUT<
ResponseBody = null,
RequestBody extends ObjectShape | null = null
> = HTTPMethod<"put", ResponseBody, RequestBody>;
RouteParams = Record<string, string | undefined>
> = RouteHandlerRules<ResponseBody, RequestBody, RouteParams>;

@@ -1,5 +0,8 @@

export interface RequestHandlerResult<ResponseBody> {
export type RequestHandlerResult<ResponseBody> = {
headers?: Record<string, string | number | boolean>;
code?: number;
body: ResponseBody;
}
} & (ResponseBody extends null ? {
body?: never;
} : {
body: ResponseBody;
})

@@ -1,2 +0,2 @@

import { HTTPMethod } from "@/@types/http-method";
import { MethodController, MethodName } from "@/@types/http-method";
import { handleRoute } from "@/util/routing";

@@ -6,18 +6,14 @@

type UnionIntersect<U, QueryParams> = (
U extends object ? (k: U) => void : object
) extends (k: infer I) => void
? I extends HTTPMethod<infer A, infer B, infer C>
? HTTPMethod<A, B, C, QueryParams>
: object
: object;
type Merge<A extends object[], QueryParams> = UnionIntersect<
A[number],
QueryParams
>;
export type RouteController<
AllowedMethods extends object[] = [],
QueryParams extends Record<string, string | undefined> = {}
> = Merge<AllowedMethods, QueryParams>;
AllowedMethods extends { [K in MethodName]?: object } | null = null,
RouteParams extends Record<string, string | undefined> = {}
> = AllowedMethods extends null
? Record<string, never>
: {
[K in keyof AllowedMethods]: AllowedMethods[K] extends MethodController<
infer A,
infer B
>
? MethodController<A, B, RouteParams>
: unknown;
};

@@ -45,3 +45,3 @@ import Koa, { Next } from "koa";

return async (context: ExtendedContext, next: Next) => {
await func(context, next);
await func(context as any, next);
await next();

@@ -48,0 +48,0 @@ };

import { RouteController } from "@/@types/route-controller";
import {
ExtendedContext,
HTTPMethodRules,
RouteHandlerRules,
MethodName,

@@ -10,3 +10,3 @@ } from "@/@types/http-method";

return Object.entries(controller).map((controllerRule) => {
const [method, rule] = controllerRule as [MethodName, HTTPMethodRules];
const [method, rule] = controllerRule as [MethodName, RouteHandlerRules];

@@ -20,5 +20,5 @@ const internalHandler = async (context: ExtendedContext) => {

const errors: string[] | null = await rule.validation
?.validate(context.request.body)
.catch(({ errors }) => errors);
const errors: string[] = await rule.validation
?.validate(context.request?.body)
.catch(({ errors }: { errors: string[] }) => errors) as string[];

@@ -32,3 +32,3 @@ if (errors?.length) {

context.status = code;
(context as unknown as { body: object | null }).body = body;
(context as unknown as { body: object | unknown }).body = body;
for (const [key, value] of Object.entries(headers))

@@ -35,0 +35,0 @@ context.set(key, value.toString());

{
"name": "18h",
"description": "A Next.js style dynamic API router for Koa-based APIs.",
"version": "2.0.7",
"version": "2.1.0",
"repository": {

@@ -6,0 +6,0 @@ "url": "https://github.com/ridafkih/18h"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc