A framework for real-time applications and REST API with JavaScript and TypeScript
npx @tessl/cli install tessl/npm-feathersjs--feathers@5.0.0Feathers is a lightweight web framework for creating APIs and real-time applications using TypeScript or JavaScript. It provides a service-oriented architecture with built-in support for hooks, events, authentication, and database integrations, enabling developers to build modern backends that support both REST APIs and real-time functionality through WebSockets.
npm install @feathersjs/feathersimport { feathers, Application } from "@feathersjs/feathers";For CommonJS:
const { feathers } = require("@feathersjs/feathers");feathers<T, S>() - Factory function that creates new Feathers application instanceFeathers - Main application classversion - Package version stringfeathers.setDebug - Debug utility functionSERVICE - Internal symbol for service metadatadefaultServiceMethods - Standard service methods arraydefaultServiceArguments - Service method argument mappingsdefaultEventMap - Service method to event name mappingsdefaultServiceEvents - Default service events arrayprotectedMethods - Protected method names arraygetHookMethods() - Get hookable service methodsgetServiceOptions() - Retrieve service configurationnormalizeServiceOptions() - Normalize service optionswrapService() - Wrap service with Feathers functionalityconvertHookData() - Convert hook registration formatscollectHooks() - Collect hooks for executionenableHooks() - Add hook capability to objectscreateContext() - Create hook execution contextFeathersHookManager - Hook management classhookMixin() - Service hook integration mixineventHook() - Core event emission hookeventMixin() - Add event capability to servicesAll TypeScript interfaces, types, and utilities for building typed Feathers applications (see Types section below).
import { feathers, Service } from "@feathersjs/feathers";
// Create a Feathers application
const app = feathers();
// Define a simple service
class MessageService implements Service {
messages = [
{ id: 1, text: "Hello world!" },
{ id: 2, text: "Welcome to Feathers!" }
];
async find() {
return this.messages;
}
async get(id: number) {
return this.messages.find(msg => msg.id === id);
}
async create(data: { text: string }) {
const message = { id: Date.now(), ...data };
this.messages.push(message);
return message;
}
}
// Register the service
app.use("messages", new MessageService());
// Set up the application
await app.setup();
// Use the service
const messages = await app.service("messages").find();
console.log(messages);Feathers is built around several key components:
Feathers class that manages services, settings, and lifecycleCore application functionality for creating Feathers instances, managing settings, and controlling application lifecycle.
function feathers<T = any, S = any>(): Application<T, S>;
interface Application<Services = any, Settings = any> extends EventEmitter {
version: string;
services: Services;
settings: Settings;
get<L extends keyof Settings & string>(name: L): Settings[L];
set<L extends keyof Settings & string>(name: L, value: Settings[L]): this;
configure(callback: (this: this, app: this) => void): this;
setup(server?: any): Promise<this>;
teardown(server?: any): Promise<this>;
}Service registration, retrieval, and lifecycle management within the Feathers application.
interface Application<Services = any, Settings = any> {
use<L extends keyof Services & string>(
path: L,
service: ServiceInterface | Application,
options?: ServiceOptions
): this;
service<L extends keyof Services & string>(path: L): FeathersService;
unuse<L extends keyof Services & string>(location: L): Promise<FeathersService>;
}
interface ServiceOptions<MethodTypes = string> {
events?: string[] | readonly string[];
methods?: MethodTypes[] | readonly MethodTypes[];
serviceEvents?: string[] | readonly string[];
routeParams?: { [key: string]: any };
}Middleware system for intercepting and modifying service method calls with support for before, after, error, and around hooks.
interface Application<Services = any, Settings = any> {
hooks(hookMap: ApplicationHookOptions<this>): this;
}
interface FeathersService<A = Application, S = Service> {
hooks(options: HookOptions<A, S>): this;
}
type HookFunction<A = Application, S = Service> = (
this: S,
context: HookContext<A, S>
) => Promise<HookContext<Application, S> | void> | HookContext<Application, S> | void;
type AroundHookFunction<A = Application, S = Service> = (
context: HookContext<A, S>,
next: NextFunction
) => Promise<void>;Standard service method interfaces and type definitions for implementing Feathers services.
interface ServiceMethods<Result = any, Data = Partial<Result>, ServiceParams = Params, PatchData = Partial<Data>> {
find(params?: ServiceParams & { paginate?: PaginationParams }): Promise<Result | Result[]>;
get(id: Id, params?: ServiceParams): Promise<Result>;
create(data: Data, params?: ServiceParams): Promise<Result>;
update(id: NullableId, data: Data, params?: ServiceParams): Promise<Result | Result[]>;
patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]>;
remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]>;
setup?(app: Application, path: string): Promise<void>;
teardown?(app: Application, path: string): Promise<void>;
}
type ServiceInterface<Result = any, Data = Partial<Result>, ServiceParams = Params, PatchData = Partial<Data>> =
Partial<ServiceMethods<Result, Data, ServiceParams, PatchData>>;type Id = number | string;
type NullableId = Id | null;
// Re-exported from @feathersjs/hooks
type NextFunction = () => Promise<void>;
interface Query {
[key: string]: any;
}
interface Params<Q = Query> {
query?: Q;
provider?: string;
route?: { [key: string]: any };
headers?: { [key: string]: any };
}
interface Paginated<T> {
total: number;
limit: number;
skip: number;
data: T[];
}
interface PaginationOptions {
default?: number;
max?: number;
}
type PaginationParams = false | PaginationOptions;interface HookContext<A = Application, S = any> {
readonly app: A;
readonly method: string;
readonly path: string;
readonly service: S;
readonly type: HookType;
readonly arguments: any[];
data?: any;
error?: any;
id?: Id;
params: Params;
result?: any;
dispatch?: any;
http?: Http;
event: string | null;
}
type HookType = 'before' | 'after' | 'error' | 'around';
interface Http {
status?: number;
headers?: { [key: string]: string | string[] };
location?: string;
}