Skip to content

Commit

Permalink
feat(rest-adapter): adds types and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 24, 2019
1 parent 665540d commit 75d9534
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
37 changes: 37 additions & 0 deletions packages/rest-adapter/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { query, error, PublicError } from '@karmic/core';
import { RESTAdapterOptions } from './types';

export default function createDefaults(): Required<RESTAdapterOptions> {
return {
crud: true,
children: true,
subscriptions: true,
declaration: '/:declaration',
context: () => ({}),
envelope(error, data) {
return error
? {
status: 'error',
error: {
id: error.id,
code: error.code,
description: error.message || null
}
}
: {
status: 'success',
data: data
};
},
notFound: query({
types: {
errors: {
NotFoundError: error({ code: 'ClientNotFound' })
}
},
async resolve() {
throw new PublicError('NotFoundError', 'ClientNotFound');
}
})
};
}
4 changes: 1 addition & 3 deletions packages/rest-adapter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export default function main(): null {
return null;
}
export * from './types';
60 changes: 60 additions & 0 deletions packages/rest-adapter/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Router, Request } from 'express';
import {
QueryServiceImplementation,
PublicError,
CollectionTreeDeclaration
} from '@karmic/core';

/**
* REST adapter output.
*/
export interface RESTAdapter {
/**
* An *express* router with the input collection's routes.
*/
router: Router;
/**
* Resulting collection declaration, including the necessary
* modifications made by the adapter.
*/
declaration: CollectionTreeDeclaration;
}

export interface RESTAdapterOptions {
/**
* Whether to modify the routes of services named `get`, `list`, `create`, `update`, `patch`, and `remove` to follow traditional RESTful conventions. Default: `true`.
*/
crud?: boolean;
/**
* Whether to create routes for type's children services. Default: `true`.
*/
children?: boolean;
/**
* Whether to create routes for subscription services, serving their first result. Default: `true`.
*/
subscriptions?: boolean;
/**
* Path to serve the collection declaration JSON at, if any. Default: `/:declaration`.
*/
declaration?: string | null;
/**
* A function to provide context before services are called.
*/
context?: RESTContextFn;
// TODO: we need to know how the types are modified by the envelope
/**
* A function returning the final data to serve.
*/
envelope?: RESTEnvelopeFn;
/**
* A service handling not found errors.
*/
notFound?: QueryServiceImplementation;
}

export type RESTContextFn<C = any> = (req: Request) => Promise<C> | Partial<C>;

export type RESTEnvelopeFn<T = any> = (
error: null | PublicError,
data: any
) => T;

0 comments on commit 75d9534

Please sign in to comment.