-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest-adapter): adds types and defaults
- Loading branch information
Showing
3 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
export default function main(): null { | ||
return null; | ||
} | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |