nbit icon indicating copy to clipboard operation
nbit copied to clipboard

built types are missing comments

Open ImLunaHey opened this issue 2 years ago • 6 comments

Here's the types that're being shipped right now with @nbit/[email protected]. Notice the lack of comments on most of the types. For example allowStaticFrom has no comment in the shipped types.

https://github.com/sstur/nbit/blob/42b19884c84c3439897da9006ea126e3d2273ccd/packages/core/src/types/http.ts#L27-L31

/// <reference types="bun-types" />
var Request$1 = Request;

var Response$1 = Response;

var Headers$1 = Headers;

var ResponseInit$1 = ResponseInit;

declare type StaticFileOptions = {
  /** Max age (in seconds) for the Cache-Control header */
  maxAge?: number;
  /** Include ETag and Last-Modified headers automatically (default: true) */
  cachingHeaders?: boolean;
};
declare class StaticFile {
  readonly filePath: string;
  readonly responseInit: ResponseInit$1;
  readonly options: StaticFileOptions;
  constructor(filePath: string, init?: ResponseInit$1 & StaticFileOptions);
}

declare type BodyStream = Request$1 extends {
  body: infer T;
}
  ? T
  : never;
declare type BodyAccessorArgs<M> = M extends MethodWithBody
  ? []
  : [ERROR: 'NO_BODY_ALLOWED_FOR_METHOD'];
declare class CustomRequest<M extends Method, Params extends string> {
  private request;
  readonly method: M;
  readonly url: string;
  readonly headers: Headers$1;
  readonly path: string;
  readonly search: string;
  readonly query: URLSearchParams;
  readonly params: {
    [K in Params]: string;
  };
  constructor(request: Request$1);
  get body(): M extends MethodWithBody ? BodyStream : never;
  get bodyUsed(): boolean;
  arrayBuffer(..._: BodyAccessorArgs<M>): Promise<ArrayBuffer>;
  text(..._: BodyAccessorArgs<M>): Promise<string>;
  json<T = JSONValue>(..._: BodyAccessorArgs<M>): Promise<T>;
}

declare type MaybePromise<T> = T | Promise<T>;
declare type MaybeIntersect<T, U> = U extends undefined ? T : T & U;
declare type JsonPayload = Record<string, unknown> | Array<unknown>;
declare type ExtractParams<T extends string> = string extends T
  ? never
  : T extends `${infer _Start}:${infer Param}/${infer Rest}`
  ? Param | ExtractParams<Rest>
  : T extends `${infer _Start}:${infer Param}`
  ? Param
  : never;
declare type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | '*';
declare type MethodWithBody = 'POST' | 'PUT';
declare type Handler<M extends Method, P extends string, RequestContext> = (
  request: MaybeIntersect<CustomRequest<M, ExtractParams<P>>, RequestContext>,
) => MaybePromise<
  Response$1 | StaticFile | JsonPayload | null | undefined | void
>;
declare type Route<RequestContext> = [
  Method,
  string,
  Handler<Method, string, RequestContext>,
];

declare type JSONValue =
  | null
  | boolean
  | number
  | string
  | Array<JSONValue>
  | {
      [key: string]: JSONValue;
    };

declare type HttpErrorInit = {
  status: number;
  message?: string;
};
declare class HttpError extends Error {
  readonly status: number;
  constructor({ status, message }: HttpErrorInit, options?: ErrorOptions);
  get name(): string;
  get [Symbol.toStringTag](): string;
}

declare class CustomResponse extends Response {
  static file(
    filePath: string,
    init?: ResponseInit & StaticFileOptions,
  ): StaticFile;
}

declare const createApplication: <
  CtxGetter extends (request: Request) => object | undefined = (
    request: Request,
  ) => undefined,
>(applicationOptions?: {
  bodyParserMaxBufferSize?: number;
  root?: string;
  allowStaticFrom?: string[];
  getContext?: CtxGetter;
}) => {
  defineRoutes: (
    fn: (app: {
      get: <P extends string>(
        path: P,
        handler: Handler<'GET', P, ReturnType<CtxGetter>>,
      ) => Route<ReturnType<CtxGetter>>;
      post: <P_1 extends string>(
        path: P_1,
        handler: Handler<'POST', P_1, ReturnType<CtxGetter>>,
      ) => Route<ReturnType<CtxGetter>>;
      put: <P_2 extends string>(
        path: P_2,
        handler: Handler<'PUT', P_2, ReturnType<CtxGetter>>,
      ) => Route<ReturnType<CtxGetter>>;
      delete: <P_3 extends string>(
        path: P_3,
        handler: Handler<'DELETE', P_3, ReturnType<CtxGetter>>,
      ) => Route<ReturnType<CtxGetter>>;
      route: <P_4 extends string>(
        method: Method,
        path: P_4,
        handler: Handler<'*', P_4, ReturnType<CtxGetter>>,
      ) => Route<ReturnType<CtxGetter>>;
    }) => Route<ReturnType<CtxGetter>>[],
  ) => Route<ReturnType<CtxGetter>>[];
  createRequestHandler: (
    ...routeLists: Route<ReturnType<CtxGetter>>[][]
  ) => (request: Request) => Promise<Response>;
  attachRoutes: (
    ...routeLists: Route<ReturnType<CtxGetter>>[][]
  ) => (request: Request) => Promise<Response>;
};

export {
  HttpError,
  Request$1 as Request,
  CustomResponse as Response,
  createApplication,
};

ImLunaHey avatar Aug 05 '23 22:08 ImLunaHey

BTW my issue isn't with comments missing overall its just some exist in the code and for some reason are being stripped out in the final type file where as others are being kept. 🤔

ImLunaHey avatar Aug 05 '23 22:08 ImLunaHey

Related I'm assuming https://github.com/microsoft/TypeScript/issues/14619

ImLunaHey avatar Aug 05 '23 22:08 ImLunaHey

You're right, this appears to be a bug. Those comments shouldn't be stripped away since they provide valuable editor hints to developers using this framework.

I'm not sure if TypeScript is stripping them or if it's Rollup, but I'll dig into this.

sstur avatar Aug 05 '23 23:08 sstur

@sstur did you happen to find anything in regards to this?

ImLunaHey avatar Sep 15 '23 10:09 ImLunaHey

I looked at this last month and came up empty. I have no idea why these comments are being stripped away by tsc. I'll try to find some time to take a closer look.

sstur avatar Sep 17 '23 22:09 sstur

If you cant find anything might I suggest using tsup even if it's just for the dts part, from what I've seen comments remain via it's dts option. 🤔

ImLunaHey avatar Sep 18 '23 05:09 ImLunaHey