UNPKG

6.21 kBTypeScriptView Raw
1import { OpaqueToken } from '@angular/core';
2import { Connection, Headers, Request, Response, ResponseOptions, URLSearchParams } from '@angular/http';
3import 'rxjs/add/operator/delay';
4/**
5* Seed data for in-memory database
6* Must implement InMemoryDbService.
7*/
8export declare const SEED_DATA: OpaqueToken;
9/**
10* Interface for a class that creates an in-memory database
11* Safe for consuming service to morph arrays and objects.
12*/
13export interface InMemoryDbService {
14 /**
15 * Creates "database" object hash whose keys are collection names
16 * and whose values are arrays of the collection objects.
17 *
18 * It must be safe to call again and should return new arrays with new objects.
19 * This condition allows InMemoryBackendService to morph the arrays and objects
20 * without touching the original source data.
21 */
22 createDb(): {};
23}
24/**
25* Interface for InMemoryBackend configuration options
26*/
27export interface InMemoryBackendConfigArgs {
28 /**
29 * default response options
30 */
31 defaultResponseOptions?: ResponseOptions;
32 /**
33 * delay (in ms) to simulate latency
34 */
35 delay?: number;
36 /**
37 * false (default) if ok when object-to-delete not found; else 404
38 */
39 delete404?: boolean;
40 /**
41 * host for this service
42 */
43 host?: string;
44 /**
45 * root path before any API call
46 */
47 rootPath?: string;
48}
49/**
50* InMemoryBackendService configuration options
51* Usage:
52* provide(InMemoryBackendConfig, {useValue: {delay:600}}),
53*/
54export declare class InMemoryBackendConfig implements InMemoryBackendConfigArgs {
55 constructor(config?: InMemoryBackendConfigArgs);
56}
57/**
58* Interface for object w/ info about the current request url
59* extracted from an Http Request
60*/
61export interface ReqInfo {
62 req: Request;
63 base: string;
64 collection: any[];
65 collectionName: string;
66 headers: Headers;
67 id: any;
68 query: URLSearchParams;
69 resourceUrl: string;
70}
71export declare const isSuccess: (status: number) => boolean;
72/**
73 * Simulate the behavior of a RESTy web api
74 * backed by the simple in-memory data store provided by the injected InMemoryDataService service.
75 * Conforms mostly to behavior described here:
76 * http://www.restapitutorial.com/lessons/httpmethods.html
77 *
78 * ### Usage
79 *
80 * Create InMemoryDataService class the implements InMemoryDataService.
81 * Register both this service and the seed data as in:
82 * ```
83 * // other imports
84 * import { HTTPPROVIDERS, XHRBackend } from 'angular2/http';
85 * import { InMemoryBackendConfig, InMemoryBackendService, SEEDDATA } from '../in-memory-backend/in-memory-backend.service';
86 * import { InMemoryStoryService } from '../api/in-memory-story.service';
87 *
88 * @Component({
89 * selector: ...,
90 * templateUrl: ...,
91 * providers: [
92 * HTTPPROVIDERS,
93 * provide(XHRBackend, { useClass: InMemoryBackendService }),
94 * provide(SEEDDATA, { useClass: InMemoryStoryService }),
95 * provide(InMemoryBackendConfig, { useValue: { delay: 600 } }),
96 * ]
97 * })
98 * export class AppComponent { ... }
99 * ```
100 */
101export declare class InMemoryBackendService {
102 private seedData;
103 protected config: InMemoryBackendConfigArgs;
104 protected db: {};
105 constructor(seedData: InMemoryDbService, config: InMemoryBackendConfigArgs);
106 createConnection(req: Request): Connection;
107 /**
108 * Process Request and return an Http Response object
109 * in the manner of a RESTy web api.
110 *
111 * Expect URI pattern in the form :base/:collectionName/:id?
112 * Examples:
113 * // for store with a 'characters' collection
114 * GET api/characters // all characters
115 * GET api/characters/42 // the character with id=42
116 * GET api/characters?name=^j // 'j' is a regex; returns characters whose name contains 'j' or 'J'
117 * GET api/characters.json/42 // ignores the ".json"
118 *
119 * POST commands/resetDb // resets the "database"
120 */
121 protected handleRequest(req: Request): Response;
122 /**
123 * Apply query/search parameters as a filter over the collection
124 * This impl only supports RegExp queries on string properties of the collection
125 * ANDs the conditions together
126 */
127 protected applyQuery(collection: any[], query: URLSearchParams): any[];
128 protected clone(data: any): any;
129 /**
130 * When the `base`="commands", the `collectionName` is the command
131 * Example URLs:
132 * commands/resetdb // Reset the "database" to its original state
133 * commands/config (GET) // Return this service's config object
134 * commands/config (!GET) // Update the config (e.g. delay)
135 *
136 * Usage:
137 * http.post('commands/resetdb', null);
138 * http.get('commands/config');
139 * http.post('commands/config', '{"delay":1000}');
140 */
141 protected commands(reqInfo: ReqInfo): ResponseOptions;
142 protected createErrorResponse(status: number, message: string): ResponseOptions;
143 protected delete({id, collection, collectionName, headers}: ReqInfo): ResponseOptions;
144 protected findById(collection: any[], id: number | string): any;
145 protected genId(collection: any): any;
146 protected get({id, query, collection, collectionName, headers}: ReqInfo): ResponseOptions;
147 protected getLocation(href: string): HTMLAnchorElement;
148 protected indexOf(collection: any[], id: number): number;
149 protected parseId(collection: {
150 id: any;
151 }[], id: string): any;
152 protected parseUrl(url: string): {
153 base: string;
154 id: string;
155 collectionName: string;
156 resourceUrl: string;
157 query: URLSearchParams;
158 };
159 protected post({collection, headers, id, req, resourceUrl}: ReqInfo): ResponseOptions;
160 protected put({id, collection, collectionName, headers, req}: ReqInfo): ResponseOptions;
161 protected removeById(collection: any[], id: number): boolean;
162 /**
163 * Reset the "database" to its original state
164 */
165 protected resetDb(): void;
166 protected setStatusText(options: ResponseOptions): ResponseOptions;
167}