From 12d0f4b9e4ce5d6771163a41ebe58cf83af342c7 Mon Sep 17 00:00:00 2001 From: Jordan Tucker Date: Mon, 13 May 2019 21:07:59 -0500 Subject: [PATCH] fix: Use `export =` in TypeScript definitions (#1285) --- packages/client/index.d.ts | 26 ++ packages/client/package.json | 1 + packages/errors/index.d.ts | 7 - packages/express/index.d.ts | 94 +++---- packages/feathers/index.d.ts | 379 ++++++++++++++-------------- packages/primus-client/index.d.ts | 23 +- packages/primus/index.d.ts | 21 +- packages/rest-client/index.d.ts | 61 ++--- packages/socketio-client/index.d.ts | 20 +- packages/socketio/index.d.ts | 26 +- tslint.json | 3 +- 11 files changed, 340 insertions(+), 321 deletions(-) create mode 100644 packages/client/index.d.ts diff --git a/packages/client/index.d.ts b/packages/client/index.d.ts new file mode 100644 index 0000000000..70dff7ed41 --- /dev/null +++ b/packages/client/index.d.ts @@ -0,0 +1,26 @@ +import feathers from '@feathersjs/feathers'; +import authentication from '@feathersjs/authentication-client'; +import errors from '@feathersjs/errors'; +import primus from '@feathersjs/primus-client'; +import rest from '@feathersjs/rest-client'; +import socketio from '@feathersjs/socketio-client'; + +export as namespace feathers; + +declare const feathersClient: FeathersClient; +export = feathersClient; + +type Feathers = typeof feathers; +type FeathersAuthenticationClient = typeof authentication; +type FeathersErrors = typeof errors; +type FeathersPrimusClient = typeof primus; +type FeathersRestClient = typeof rest; +type FeathersSocketIOClient = typeof socketio; + +interface FeathersClient extends Feathers { + authentication: FeathersAuthenticationClient; + errors: FeathersErrors; + primus: FeathersPrimusClient; + rest: FeathersRestClient; + socketio: FeathersSocketIOClient; +} diff --git a/packages/client/package.json b/packages/client/package.json index 90afae083f..19562913f5 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -20,6 +20,7 @@ "node": ">= 6" }, "main": "index.js", + "types": "index.d.ts", "scripts": { "clean": "../../node_modules/.bin/shx rm -rf dist/ && ../../node_modules/.bin/shx mkdir -p dist", "version": "npm run build", diff --git a/packages/errors/index.d.ts b/packages/errors/index.d.ts index 29189622c3..7b79548a2e 100644 --- a/packages/errors/index.d.ts +++ b/packages/errors/index.d.ts @@ -1,10 +1,3 @@ -// Type definitions for @feathersjs/errors 3.3 -// Project: https://feathersjs.com -// Definitions by: Jan Lohage -// RazzM13 -// Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript -// TypeScript Version: 2.2 - export interface FeathersErrorJSON { readonly name: string; readonly message: string; diff --git a/packages/express/index.d.ts b/packages/express/index.d.ts index 5bc8b9f680..1ab9bbcfae 100644 --- a/packages/express/index.d.ts +++ b/packages/express/index.d.ts @@ -1,58 +1,38 @@ -// Type definitions for @feathersjs/express 1.1 -// Project: https://feathersjs.com -// Definitions by: Jan Lohage -// Aleksey Klimenko -// Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript -// TypeScript Version: 2.3 - import { Application as FeathersApplication } from '@feathersjs/feathers'; -import * as express from 'express'; - -declare const feathersExpress: ((app: FeathersApplication) => Application); -export default feathersExpress; -export type Application = express.Application & FeathersApplication; - -export function errorHandler (options?: { - public?: string, - logger?: { error?: (msg: string) => void }|null, - html?: any, - json?: any -}): express.ErrorRequestHandler; -export function notFound (): express.RequestHandler; - -export const rest: { - (handler?: express.RequestHandler): () => void; - formatter: express.RequestHandler; -}; - -/* - * Re-export of the express package. - **/ - -export { - CookieOptions, - Errback, - ErrorRequestHandler, - Express, - Handler, - IRoute, - IRouter, - IRouterHandler, - IRouterMatcher, - json, - MediaType, - NextFunction, - Request, - RequestHandler, - RequestParamHandler, - Response, - Router, - RouterOptions, - Send, - static, - urlencoded -} from 'express'; - -export function parseAuthentication (...strategies: string[]): express.RequestHandler; -export function authenticate (...strategies: string[]): express.RequestHandler; -export const original: () => express.Application; +import express from 'express'; + +declare const feathersExpress: FeathersExpress; +export = feathersExpress; + +type Express = typeof express; + +interface FeathersExpress extends Express { + (app: FeathersApplication): feathersExpress.Application; + + (app?: any): express.Express; + + default: FeathersExpress; + + rest: { + (handler?: express.RequestHandler): () => void; + formatter: express.RequestHandler; + }; + + original: Express; + + errorHandler (options?: { + public?: string, + logger?: { error?: (msg: string) => void | null }, + html?: any, + json?: any + }): express.ErrorRequestHandler; + + notFound (): express.RequestHandler; + + parseAuthentication (...strategies: string[]): express.RequestHandler; + authenticate (...strategies: string[]): express.RequestHandler; +} + +declare namespace feathersExpress { + type Application = express.Express & FeathersApplication; +} diff --git a/packages/feathers/index.d.ts b/packages/feathers/index.d.ts index 9273162581..931d86a4ab 100644 --- a/packages/feathers/index.d.ts +++ b/packages/feathers/index.d.ts @@ -1,212 +1,213 @@ -// Type definitions for @feathersjs/feathers 3.1 -// Project: http://feathersjs.com/ -// Definitions by: Jan Lohage -// Abraao Alves -// Tim Mensch -// Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript - -// TypeScript Version: 2.3 - /// import { EventEmitter } from 'events'; -// tslint:disable-next-line no-unnecessary-generics -declare const feathers: (() => Application); -export default feathers; - -export const version: string; - -export type Id = number | string; -export type NullableId = Id | null; - -export interface Query { - [key: string]: any; -} - -export interface PaginationOptions { - default: number; - max: number; -} - -export type ClientSideParams = Pick; -export type ServerSideParams = Params; - -export interface Params { - query?: Query; - paginate?: false | Pick; - - [key: string]: any; // (JL) not sure if we want this -} - -export interface Paginated { - total: number; - limit: number; - skip: number; - data: T[]; -} - -// tslint:disable-next-line void-return -export type Hook = (hook: HookContext) => (Promise | HookContext | void); - -export interface HookContext { - /** - * A read only property that contains the Feathers application object. This can be used to - * retrieve other services (via context.app.service('name')) or configuration values. - */ - readonly app: Application; - /** - * A writeable property containing the data of a create, update and patch service - * method call. - */ - data?: T; - /** - * A writeable property with the error object that was thrown in a failed method call. - * It is only available in error hooks. - */ - error?: any; - /** - * A writeable property and the id for a get, remove, update and patch service - * method call. For remove, update and patch context.id can also be null when - * modifying multiple entries. In all other cases it will be undefined. - */ - id?: string | number; - /** - * A read only property with the name of the service method (one of find, get, - * create, update, patch, remove). - */ - readonly method: string; - /** - * A writeable property that contains the service method parameters (including - * params.query). - */ - params: Params; - /** - * A read only property and contains the service name (or path) without leading or - * trailing slashes. - */ - readonly path: string; - /** - * A writeable property containing the result of the successful service method call. - * It is only available in after hooks. - * - * `context.result` can also be set in - * - * - A before hook to skip the actual service method (database) call - * - An error hook to swallow the error and return a result instead - */ - result?: T; - /** - * A read only property and contains the service this hook currently runs on. - */ - readonly service: Service; - /** - * A writeable, optional property and contains a 'safe' version of the data that - * should be sent to any client. If context.dispatch has not been set context.result - * will be sent to the client instead. - */ - dispatch?: T; - /** - * A writeable, optional property that allows to override the standard HTTP status - * code that should be returned. - */ - statusCode?: number; - /** - * A read only property with the hook type (one of before, after or error). - */ - readonly type: 'before' | 'after' | 'error'; - /** - * The real-time connection object - */ - connection?: any; -} - -export interface HookMap { - all: Hook | Hook[]; - find: Hook | Hook[]; - get: Hook | Hook[]; - create: Hook | Hook[]; - update: Hook | Hook[]; - patch: Hook | Hook[]; - remove: Hook | Hook[]; -} - -export interface HooksObject { - before: Partial | Hook | Hook[]; - after: Partial | Hook | Hook[]; - error: Partial | Hook | Hook[]; - finally: Partial | Hook | Hook[]; -} - -// todo: figure out what to do: These methods don't actually need to be implemented, so they can be undefined at runtime. Yet making them optional gets cumbersome in strict mode. -export interface ServiceMethods { - [key: string]: any; - - find? (params?: Params): Promise>; - - get? (id: Id, params?: Params): Promise; - - create? (data: Partial | Array>, params?: Params): Promise; +declare const createApplication: Feathers; +export = createApplication; - update? (id: NullableId, data: T, params?: Params): Promise; - - patch? (id: NullableId, data: Partial, params?: Params): Promise; - - remove? (id: NullableId, params?: Params): Promise; -} - -export interface SetupMethod { - setup (app: Application, path: string): void; -} - -export interface ServiceOverloads { - create? (data: Array>, params?: Params): Promise; - - create? (data: Partial, params?: Params): Promise; - - patch? (id: NullableId, data: Pick, params?: Params): Promise; -} - -export interface ServiceAddons extends EventEmitter { - id?: any; - _serviceEvents: string[]; - hooks (hooks: Partial): this; +interface Feathers { + (): createApplication.Application; + readonly ACTIVATE_HOOKS: unique symbol; + version: string; + default: Feathers; + // TODO: Write a definition for activateHooks. + // activateHooks(): void } -export type Service = ServiceOverloads & ServiceAddons & ServiceMethods; - -export type ServiceMixin = (service: Service, path: string) => void; - -export interface Application extends EventEmitter { - version: string; +declare namespace createApplication { + type Id = number | string; + type NullableId = Id | null; + + interface Query { + [key: string]: any; + } + + interface PaginationOptions { + default: number; + max: number; + } + + type ClientSideParams = Pick; + type ServerSideParams = Params; + + interface Params { + query?: Query; + paginate?: false | Pick; + + [key: string]: any; // (JL) not sure if we want this + } + + interface Paginated { + total: number; + limit: number; + skip: number; + data: T[]; + } + + // tslint:disable-next-line void-return + type Hook = (hook: HookContext) => (Promise | HookContext | void); + + interface HookContext { + /** + * A read only property that contains the Feathers application object. This can be used to + * retrieve other services (via context.app.service('name')) or configuration values. + */ + readonly app: Application; + /** + * A writeable property containing the data of a create, update and patch service + * method call. + */ + data?: T; + /** + * A writeable property with the error object that was thrown in a failed method call. + * It is only available in error hooks. + */ + error?: any; + /** + * A writeable property and the id for a get, remove, update and patch service + * method call. For remove, update and patch context.id can also be null when + * modifying multiple entries. In all other cases it will be undefined. + */ + id?: string | number; + /** + * A read only property with the name of the service method (one of find, get, + * create, update, patch, remove). + */ + readonly method: string; + /** + * A writeable property that contains the service method parameters (including + * params.query). + */ + params: Params; + /** + * A read only property and contains the service name (or path) without leading or + * trailing slashes. + */ + readonly path: string; + /** + * A writeable property containing the result of the successful service method call. + * It is only available in after hooks. + * + * `context.result` can also be set in + * + * - A before hook to skip the actual service method (database) call + * - An error hook to swallow the error and return a result instead + */ + result?: T; + /** + * A read only property and contains the service this hook currently runs on. + */ + readonly service: Service; + /** + * A writeable, optional property and contains a 'safe' version of the data that + * should be sent to any client. If context.dispatch has not been set context.result + * will be sent to the client instead. + */ + dispatch?: T; + /** + * A writeable, optional property that allows to override the standard HTTP status + * code that should be returned. + */ + statusCode?: number; + /** + * A read only property with the hook type (one of before, after or error). + */ + readonly type: 'before' | 'after' | 'error'; + /** + * The real-time connection object + */ + connection?: any; + } + + interface HookMap { + all: Hook | Hook[]; + find: Hook | Hook[]; + get: Hook | Hook[]; + create: Hook | Hook[]; + update: Hook | Hook[]; + patch: Hook | Hook[]; + remove: Hook | Hook[]; + } + + interface HooksObject { + before: Partial | Hook | Hook[]; + after: Partial | Hook | Hook[]; + error: Partial | Hook | Hook[]; + finally: Partial | Hook | Hook[]; + } + + // todo: figure out what to do: These methods don't actually need to be + // implemented, so they can be undefined at runtime. Yet making them + // optional gets cumbersome in strict mode. + interface ServiceMethods { + [key: string]: any; + + find? (params?: Params): Promise>; + + get? (id: Id, params?: Params): Promise; + + create? (data: Partial | Array>, params?: Params): Promise; + + update? (id: NullableId, data: T, params?: Params): Promise; + + patch? (id: NullableId, data: Partial, params?: Params): Promise; + + remove? (id: NullableId, params?: Params): Promise; + } + + interface SetupMethod { + setup (app: Application, path: string): void; + } + + interface ServiceOverloads { + create? (data: Array>, params?: Params): Promise; + + create? (data: Partial, params?: Params): Promise; + + patch? (id: NullableId, data: Pick, params?: Params): Promise; + } + + interface ServiceAddons extends EventEmitter { + id?: any; + _serviceEvents: string[]; + hooks (hooks: Partial): this; + } + + type Service = ServiceOverloads & ServiceAddons & ServiceMethods; + + type ServiceMixin = (service: Service, path: string) => void; + + interface Application extends EventEmitter { + version: string; - services: ServiceTypes; + services: ServiceTypes; - mixins: ServiceMixin[]; + mixins: ServiceMixin[]; - methods: string[]; + methods: string[]; - get (name: string): any; + get (name: string): any; - set (name: string, value: any): this; + set (name: string, value: any): this; - disable (name: string): this; + disable (name: string): this; - disabled (name: string): boolean; + disabled (name: string): boolean; - enable (name: string): this; + enable (name: string): this; - enabled (name: string): boolean; + enabled (name: string): boolean; - configure (callback: (this: this, app: this) => void): this; + configure (callback: (this: this, app: this) => void): this; - hooks (hooks: Partial): this; + hooks (hooks: Partial): this; - setup (server?: any): this; + setup (server?: any): this; - service (location: L): ServiceTypes[L]; + service (location: L): ServiceTypes[L]; - service (location: string): Service; + service (location: string): Service; - use (path: string, service: Partial & SetupMethod> | Application, options?: any): this; + use (path: string, service: Partial & SetupMethod> | Application, options?: any): this; + } } diff --git a/packages/primus-client/index.d.ts b/packages/primus-client/index.d.ts index 3c07b75e53..d394ee41ec 100644 --- a/packages/primus-client/index.d.ts +++ b/packages/primus-client/index.d.ts @@ -1,12 +1,17 @@ -// Type definitions for @feathersjs/primus-client 1.0 -// Project: https://feathersjs.com -// Definitions by: Jan Lohage -// Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript -// TypeScript Version: 2.3 +// Primus removed its typings from the repo +// https://github.com/primus/primus/pull/623, as of 01/2018 there are none on +// DefinitelyTyped. -// primus removed its typings from the repo https://github.com/primus/primus/pull/623, as of 01/2018 there are none on DT -export default function feathersPrimusClient (socket: any, options?: FeathersPrimusClientOptions): () => void; +declare const primusClient: FeathersPrimusClient; +export = primusClient; -export interface FeathersPrimusClientOptions { - timeout?: number; +interface FeathersPrimusClient { + (socket: any, options?: primusClient.Options): () => void; + default: FeathersPrimusClient; +} + +declare namespace primusClient { + interface Options { + timeout?: number; + } } diff --git a/packages/primus/index.d.ts b/packages/primus/index.d.ts index daaccdc0e2..3815db09d3 100644 --- a/packages/primus/index.d.ts +++ b/packages/primus/index.d.ts @@ -1,13 +1,20 @@ -// Definitions by: Jan Lohage -/// +// Primus removed its typings from the repo +// https://github.com/primus/primus/pull/623, as of 01/2018 there are none on +// DefinitelyTyped. -import Http from 'http'; +import http from 'http'; + +declare const configurePrimus: FeathersPrimus; +export = configurePrimus; + +interface FeathersPrimus { + (options: any, callback?: (primus: any) => void): () => void; + readonly SOCKET_KEY: unique symbol; + default: FeathersPrimus; +} declare module '@feathersjs/feathers' { interface Application { - listen (port: number): Http.Server; + listen (port: number): http.Server; } } - -// primus removed its typings from the repo https://github.com/primus/primus/pull/623, as of 01/2018 there are none on DT -export default function feathersPrimus (options: any, callback?: (primus: any) => void): () => void; diff --git a/packages/rest-client/index.d.ts b/packages/rest-client/index.d.ts index b9e1b057c4..54f574b968 100644 --- a/packages/rest-client/index.d.ts +++ b/packages/rest-client/index.d.ts @@ -1,37 +1,40 @@ -// Type definitions for @feathersjs/rest-client 1.3 -// Project: https://feathersjs.com -// Definitions by: Jan Lohage -// Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript - // todo: get rid of all the anys -export default function feathersRestClient (base?: string): Transport; +declare const restClient: FeathersRestClient; +export = restClient; + +interface FeathersRestClient { + (base?: string): restClient.Transport; + default: FeathersRestClient; +} -export interface HandlerResult extends Function { - /** - * initialize service - */ - (): void; +declare namespace restClient { + interface HandlerResult extends Function { + /** + * initialize service + */ + (): void; - /** - * Transport Service - */ - Service: any; + /** + * Transport Service + */ + Service: any; - /** - * default Service - */ - service: any; -} + /** + * default Service + */ + service: any; + } -export type Handler = (connection: any, options?: any) => () => HandlerResult; + type Handler = (connection: any, options?: any) => () => HandlerResult; -export interface Transport { - jquery: Handler; - superagent: Handler; - request: Handler; - fetch: Handler; - axios: Handler; - angular: Handler; - angularHttpClient: Handler; + interface Transport { + jquery: Handler; + superagent: Handler; + request: Handler; + fetch: Handler; + axios: Handler; + angular: Handler; + angularHttpClient: Handler; + } } diff --git a/packages/socketio-client/index.d.ts b/packages/socketio-client/index.d.ts index 4da2ab7027..770112ec7b 100644 --- a/packages/socketio-client/index.d.ts +++ b/packages/socketio-client/index.d.ts @@ -1,13 +1,15 @@ -// Type definitions for @feathersjs/socketio-client 1.0 -// Project: https://feathersjs.com -// Definitions by: Jan Lohage -// Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript -// TypeScript Version: 2.3 +import 'socket.io-client'; -/// +declare const socketioClient: FeathersSocketIOClient; +export = socketioClient; -export default function feathersSocketIOClient (socket: SocketIOClient.Socket, options?: FeathersSocketIOClientOptions): () => void; +interface FeathersSocketIOClient { + (socket: SocketIOClient.Socket, options?: socketioClient.Options): () => void; + default: FeathersSocketIOClient; +} -export interface FeathersSocketIOClientOptions { - timeout?: number; +declare namespace socketioClient { + interface Options { + timeout?: number; + } } diff --git a/packages/socketio/index.d.ts b/packages/socketio/index.d.ts index 47f1a32d25..68e0b6302e 100644 --- a/packages/socketio/index.d.ts +++ b/packages/socketio/index.d.ts @@ -1,19 +1,19 @@ -// Type definitions for @feathersjs/socketio 3.0 -// Project: https://feathersjs.com -// Definitions by: Jan Lohage -// Definitions: https://github.com/feathersjs-ecosystem/feathers-typescript -// TypeScript Version: 2.3 +import http from 'http'; +import io from 'socket.io'; -/// -import Http from 'http'; -import SocketIO from 'socket.io'; +declare const socketio: FeathersSocketIO; +export = socketio; + +interface FeathersSocketIO { + (callback?: (io: io.Server) => void): () => void; + (options: number | io.ServerOptions, callback?: (io: io.Server) => void): () => void; + (port: number, options?: io.ServerOptions, callback?: (io: io.Server) => void): () => void; + readonly SOCKET_KEY: unique symbol; + default: FeathersSocketIO; +} declare module '@feathersjs/feathers' { interface Application { - listen (port: number): Http.Server; + listen (port: number): http.Server; } } - -export default function feathersSocketIO (callback?: (io: SocketIO.Server) => void): () => void; -export default function feathersSocketIO (options: number | SocketIO.ServerOptions, callback?: (io: SocketIO.Server) => void): () => void; -export default function feathersSocketIO (port: number, options?: SocketIO.ServerOptions, callback?: (io: SocketIO.Server) => void): () => void; diff --git a/tslint.json b/tslint.json index e114482ae3..462ca5bfbf 100644 --- a/tslint.json +++ b/tslint.json @@ -33,7 +33,8 @@ "max-classes-per-file": false, "only-arrow-functions": false, "no-empty": false, - "no-shadowed-variable": false + "no-shadowed-variable": false, + "no-namespace": [ true, "allow-declarations" ] }, "jsRules": true, "rulesDirectory": []