UNPKG

8.26 kBTypeScriptView Raw
1import { Observable } from 'rxjs';
2/**
3 * Minimum definition needed by base class
4 */
5export interface HeadersCore {
6 set(name: string, value: string): void | any;
7}
8/**
9* Interface for a class that creates an in-memory database
10*
11* Its `createDb` method creates a hash of named collections that represents the database
12*
13* For maximum flexibility, the service may define HTTP method overrides.
14* Such methods must match the spelling of an HTTP method in lower case (e.g, "get").
15* If a request has a matching method, it will be called as in
16* `get(info: requestInfo, db: {})` where `db` is the database object described above.
17*/
18export declare abstract class InMemoryDbService {
19 /**
20 * Creates an in-memory "database" hash whose keys are collection names
21 * and whose values are arrays of collection objects to return or update.
22 *
23 * returns Observable of the database because could have to create it asynchronously.
24 *
25 * This method must be safe to call repeatedly.
26 * Each time it should return a new object with new arrays containing new item objects.
27 * This condition allows the in-memory backend service to mutate the collections
28 * and their items without touching the original source data.
29 *
30 * The in-mem backend service calls this method without a value the first time.
31 * The service calls it with the `RequestInfo` when it receives a POST `commands/resetDb` request.
32 * Your InMemoryDbService can adjust its behavior accordingly.
33 */
34 abstract createDb(reqInfo?: RequestInfo): {} | Observable<{}> | Promise<{}>;
35}
36/**
37* Interface for InMemoryBackend configuration options
38*/
39export declare abstract class InMemoryBackendConfigArgs {
40 /**
41 * The base path to the api, e.g, 'api/'.
42 * If not specified than `parseRequestUrl` assumes it is the first path segment in the request.
43 */
44 apiBase?: string;
45 /**
46 * false (default) if search match should be case insensitive
47 */
48 caseSensitiveSearch?: boolean;
49 /**
50 * false (default) put content directly inside the response body.
51 * true: encapsulate content in a `data` property inside the response body, `{ data: ... }`.
52 */
53 dataEncapsulation?: boolean;
54 /**
55 * delay (in ms) to simulate latency
56 */
57 delay?: number;
58 /**
59 * false (default) should 204 when object-to-delete not found; true: 404
60 */
61 delete404?: boolean;
62 /**
63 * host for this service, e.g., 'localhost'
64 */
65 host?: string;
66 /**
67 * false (default) should pass unrecognized request URL through to original backend; true: 404
68 */
69 passThruUnknownUrl?: boolean;
70 /**
71 * true (default) should NOT return the item (204) after a POST. false: return the item (200).
72 */
73 post204?: boolean;
74 /**
75 * false (default) should NOT update existing item with POST. false: OK to update.
76 */
77 post409?: boolean;
78 /**
79 * true (default) should NOT return the item (204) after a POST. false: return the item (200).
80 */
81 put204?: boolean;
82 /**
83 * false (default) if item not found, create as new item; false: should 404.
84 */
85 put404?: boolean;
86 /**
87 * root path _before_ any API call, e.g., ''
88 */
89 rootPath?: string;
90}
91/**
92* InMemoryBackendService configuration options
93* Usage:
94* InMemoryWebApiModule.forRoot(InMemHeroService, {delay: 600})
95*
96* or if providing separately:
97* provide(InMemoryBackendConfig, {useValue: {delay: 600}}),
98*/
99export declare class InMemoryBackendConfig implements InMemoryBackendConfigArgs {
100 constructor(config?: InMemoryBackendConfigArgs);
101}
102/** Return information (UriInfo) about a URI */
103export declare function parseUri(str: string): UriInfo;
104/**
105 *
106 * Interface for the result of the `parseRequestUrl` method:
107 * Given URL "http://localhost:8080/api/customers/42?foo=1 the default implementation returns
108 * base: 'api/'
109 * collectionName: 'customers'
110 * id: '42'
111 * query: this.createQuery('foo=1')
112 * resourceUrl: 'http://localhost/api/customers/'
113 */
114export interface ParsedRequestUrl {
115 apiBase: string;
116 collectionName: string;
117 id: string;
118 query: Map<string, string[]>;
119 resourceUrl: string;
120}
121export interface PassThruBackend {
122 /**
123 * Handle an HTTP request and return an Observable of HTTP response
124 * Both the request type and the response type are determined by the supporting HTTP library.
125 */
126 handle(req: any): Observable<any>;
127}
128export declare function removeTrailingSlash(path: string): string;
129/**
130 * Minimum definition needed by base class
131 */
132export interface RequestCore {
133 url: string;
134 urlWithParams?: string;
135}
136/**
137* Interface for object w/ info about the current request url
138* extracted from an Http Request.
139* Also holds utility methods and configuration data from this service
140*/
141export interface RequestInfo {
142 req: RequestCore;
143 apiBase: string;
144 collectionName: string;
145 collection: any;
146 headers: HeadersCore;
147 method: string;
148 id: any;
149 query: Map<string, string[]>;
150 resourceUrl: string;
151 url: string;
152 utils: RequestInfoUtilities;
153}
154/**
155 * Interface for utility methods from this service instance.
156 * Useful within an HTTP method override
157 */
158export interface RequestInfoUtilities {
159 /**
160 * Create a cold response Observable from a factory for ResponseOptions
161 * the same way that the in-mem backend service does.
162 * @param resOptionsFactory - creates ResponseOptions when observable is subscribed
163 * @param withDelay - if true (default), add simulated latency delay from configuration
164 */
165 createResponse$: (resOptionsFactory: () => ResponseOptions) => Observable<any>;
166 /**
167 * Find first instance of item in collection by `item.id`
168 * @param collection
169 * @param id
170 */
171 findById<T extends {
172 id: any;
173 }>(collection: T[], id: any): T;
174 /** return the current, active configuration which is a blend of defaults and overrides */
175 getConfig(): InMemoryBackendConfigArgs;
176 /** Get the in-mem service's copy of the "database" */
177 getDb(): {};
178 /** Get JSON body from the request object */
179 getJsonBody(req: any): any;
180 /** Get location info from a url, even on server where `document` is not defined */
181 getLocation(url: string): UriInfo;
182 /** Get (or create) the "real" backend */
183 getPassThruBackend(): PassThruBackend;
184 /**
185 * return true if can determine that the collection's `item.id` is a number
186 * */
187 isCollectionIdNumeric<T extends {
188 id: any;
189 }>(collection: T[], collectionName: string): boolean;
190 /**
191 * Parses the request URL into a `ParsedRequestUrl` object.
192 * Parsing depends upon certain values of `config`: `apiBase`, `host`, and `urlRoot`.
193 */
194 parseRequestUrl(url: string): ParsedRequestUrl;
195}
196/**
197 * Provide a `responseInterceptor` method of this type in your `inMemDbService` to
198 * morph the response options created in the `collectionHandler`.
199 */
200export declare type ResponseInterceptor = (res: ResponseOptions, ri: RequestInfo) => ResponseOptions;
201export interface ResponseOptions {
202 /**
203 * String, Object, ArrayBuffer or Blob representing the body of the {@link Response}.
204 */
205 body?: string | Object | ArrayBuffer | Blob;
206 /**
207 * Response headers
208 */
209 headers?: HeadersCore;
210 /**
211 * Http {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html status code}
212 * associated with the response.
213 */
214 status?: number;
215 /**
216 * Status text for the status code
217 */
218 statusText?: string;
219 /**
220 * request url
221 */
222 url?: string;
223}
224/** Interface of information about a Uri */
225export interface UriInfo {
226 source: string;
227 protocol: string;
228 authority: string;
229 userInfo: string;
230 user: string;
231 password: string;
232 host: string;
233 port: string;
234 relative: string;
235 path: string;
236 directory: string;
237 file: string;
238 query: string;
239 anchor: string;
240}