|
| 1 | +/** |
| 2 | + * @template T, ID |
| 3 | + */ |
| 4 | +export class RestService<T, ID> { |
| 5 | + /** |
| 6 | + * @param {ng.HttpService} $http Angular-like $http service |
| 7 | + * @param {string} baseUrl Base URL template, e.g. "/users/:id{/subId}{?page,limit}" |
| 8 | + * @param {{new(data: any): T}=} entityClass Optional constructor for mapping JSON to class instances |
| 9 | + * @param {Object=} providerDefaults Optional defaults from RestProvider |
| 10 | + */ |
| 11 | + constructor( |
| 12 | + $http: ng.HttpService, |
| 13 | + baseUrl: string, |
| 14 | + entityClass?: |
| 15 | + | { |
| 16 | + new (data: any): T; |
| 17 | + } |
| 18 | + | undefined, |
| 19 | + providerDefaults?: any | undefined, |
| 20 | + ); |
| 21 | + /** @private @type {ng.HttpService} */ |
| 22 | + private $http; |
| 23 | + /** @private @type {string} */ |
| 24 | + private baseUrl; |
| 25 | + /** @private @type {{new(data: any): T}=} */ |
| 26 | + private entityClass; |
| 27 | + /** @type {Object} global defaults from provider */ |
| 28 | + providerDefaults: any; |
| 29 | + /** @type {Array<(config: any) => any | Promise<any>>} */ |
| 30 | + requestInterceptors: Array<(config: any) => any | Promise<any>>; |
| 31 | + /** @type {Array<(response: any) => any | Promise<any>>} */ |
| 32 | + responseInterceptors: Array<(response: any) => any | Promise<any>>; |
| 33 | + /** |
| 34 | + * Apply all request interceptors sequentially |
| 35 | + * @private |
| 36 | + */ |
| 37 | + private _applyRequestInterceptors; |
| 38 | + /** |
| 39 | + * Apply all response interceptors sequentially |
| 40 | + * @private |
| 41 | + */ |
| 42 | + private _applyResponseInterceptors; |
| 43 | + /** |
| 44 | + * @private |
| 45 | + */ |
| 46 | + private _request; |
| 47 | + /** @private map raw data to entity class */ |
| 48 | + private mapEntity; |
| 49 | + /** |
| 50 | + * @private |
| 51 | + * Build URL by replacing colon-style params first, then expanding RFC 6570 template |
| 52 | + * @param {string} template |
| 53 | + * @param {Record<string, any>} [params] |
| 54 | + * @returns {string} |
| 55 | + */ |
| 56 | + private buildUrl; |
| 57 | + /** List entities (optional query params) */ |
| 58 | + list(params?: {}): Promise<any>; |
| 59 | + /** Read entity by ID (ID can be in colon or RFC template) */ |
| 60 | + read(id: any, params?: {}): Promise<any>; |
| 61 | + create(item: any, params?: {}): Promise<any>; |
| 62 | + update(id: any, item: any, params?: {}): Promise<any>; |
| 63 | + delete(id: any, params?: {}): Promise<boolean>; |
| 64 | +} |
| 65 | +/** |
| 66 | + * RestProvider - register named rest stores at config time. |
| 67 | + * |
| 68 | + * Usage (in config): |
| 69 | + * restProvider.rest('user', '/api/users', User); |
| 70 | + * |
| 71 | + * Then at runtime you can inject `rest` factory and do: |
| 72 | + * const userApi = rest('/api/users', User); |
| 73 | + * or use the pre-registered named services: |
| 74 | + * const userApi = rest.get('user'); |
| 75 | + */ |
| 76 | +export class RestProvider { |
| 77 | + /** @private @type {import('./interface.ts').RestDefinition[]} */ |
| 78 | + private definitions; |
| 79 | + /** provider-level defaults (optional) */ |
| 80 | + defaults: {}; |
| 81 | + /** |
| 82 | + * Register a named rest definition during configtime |
| 83 | + * @template T |
| 84 | + * @param {string} name |
| 85 | + * @param {string} url |
| 86 | + * @param {{new(data:any):T}=} entityClass |
| 87 | + */ |
| 88 | + rest<T>( |
| 89 | + name: string, |
| 90 | + url: string, |
| 91 | + entityClass?: |
| 92 | + | { |
| 93 | + new (data: any): T; |
| 94 | + } |
| 95 | + | undefined, |
| 96 | + ): void; |
| 97 | + /** |
| 98 | + * $get factory: returns a `rest` factory function and allows access |
| 99 | + * to pre-registered services via rest.get(name). |
| 100 | + * |
| 101 | + * @returns {(baseUrl:string, entityClass?:Function, options?:object) => RestService} |
| 102 | + */ |
| 103 | + $get: ( |
| 104 | + | string |
| 105 | + | (($http: any) => { |
| 106 | + (baseUrl: any, entityClass: any, options: any): RestService<any, any>; |
| 107 | + get(name: any): any; |
| 108 | + listNames(): any[]; |
| 109 | + }) |
| 110 | + )[]; |
| 111 | +} |
0 commit comments