UNPKG

5.14 kBTypeScriptView Raw
1import { HttpClient } from '@angular/common/http';
2import { Router } from '@angular/router';
3import { Store } from '@ngrx/store';
4import { Observable } from 'rxjs/Observable';
5import 'rxjs/add/operator/share';
6import 'rxjs/add/operator/catch';
7import 'rxjs/add/operator/map';
8import 'rxjs/add/observable/of';
9export declare class ApiHttpService {
10 private httpSvc;
11 private storeSvc;
12 private routerSvc;
13 /** Hold GET requests from an API using the URL as a primary key */
14 protected cache: {
15 [key: string]: Observable<any>;
16 };
17 constructor(httpSvc: HttpClient, storeSvc: Store<IStore.root>, routerSvc: Router);
18 /**
19 * Make a GET request with simple caching
20 * @param url - The URL location of the webapi
21 * @param updateCache - Refresh the version in the cache
22 */
23 get<T>(url: string, updateCache?: boolean): Observable<T>;
24 /**
25 * Make a GET request and load the results into the store
26 * @param url - The URL location of the webapi
27 * @param id - The location to put the results in the store
28 * @param updateCache - Refresh the version in the cache
29 */
30 protected getStore<T>(url: string, apiMap?: IStore.ApiMap, updateCache?: boolean): Observable<T>;
31 /**
32 * Make a POST request and load the results into the store
33 * @param url - The URL location of the endpoint
34 * @param id - The location to put the results in the store
35 * @param data - The data to pass to the server
36 */
37 protected postStore<T>(url: string, apiMap: IStore.ApiMap, data: any): Observable<T>;
38 /**
39 * Make a PUT request
40 * @param url - The URL location of the webapi
41 * @param data - The data to pass to the server
42 */
43 protected putStore<T>(url: string, apiMap: IStore.ApiMap, data: any): Observable<T>;
44 /**
45 * Make a DELETE request
46 * @param url - The URL location of the webapi
47 * @param apiMap - The ApiMap object
48 * @param element - The element or collection of elements being deleted
49 */
50 protected deleteStore<T>(url: string, apiMap: IStore.ApiMap, element: any | any[]): Observable<T>;
51 /**
52 * When an authentication check fails
53 * @param error
54 */
55 private endSession(error);
56}
57export declare namespace IStore {
58 /*************************
59 * App specific interfaces
60 *************************/
61 /** API Store */
62 interface api {
63 users?: Mapped<{
64 user: string;
65 email: string;
66 name: string;
67 phone: string;
68 username: string;
69 website: string;
70 }>;
71 }
72 /** The API Map */
73 interface ApiMapping {
74 users?: ApiMap;
75 }
76 /** UI Store */
77 interface ui {
78 modal?: {
79 modalId: string;
80 options: {};
81 data: any;
82 };
83 }
84 /*************************
85 * Non-customizable interfaces
86 *************************/
87 /** The root store which contains the other stores */
88 interface root {
89 api?: api;
90 ui?: ui;
91 apiStatus?: apiStatus;
92 }
93 /** API status store */
94 interface apiStatus {
95 [key: string]: ApiStatus;
96 }
97 /** Example pattern for data that is mapped before being passed into the store */
98 interface Mapped<T> {
99 /** Unaltered source of API response */
100 src?: T[];
101 /** A dictionary organized by the primary key */
102 dict?: {
103 [key: string]: T;
104 };
105 /** A deduped array arranged into a dictionary by primary key */
106 uniques?: {
107 [key: string]: T;
108 };
109 }
110 interface StateStatuses {
111 users?: ApiStatus;
112 }
113 interface ApiStatus {
114 loading?: boolean;
115 loaded?: boolean;
116 loadError?: any;
117 modifying?: boolean;
118 modified?: boolean;
119 modifyError?: any;
120 }
121 /** Maps the relationship between the store and the API. Automates all the interaction. */
122 interface ApiMap {
123 /** The location of the rest API endpoint */
124 endpoint?: string;
125 /** The location/property of where to put the API response into the store */
126 storeProperty?: string;
127 /** A unique ID of each object in the collection. Also supports an array of strings if multiple unique ID's are needed in the event of a single key not being enough. */
128 uniqueId?: string | string[];
129 /** A callback function to modify the API response before it is inserted into the store */
130 map?: any;
131 /** If a map callback function is specified, this is the key for the location of the original unfiltered list of items. This is necessary to update the mapped list in the store without a GET all */
132 mapSrc?: string;
133 /** Occasionally a unique piece of information needs to be passed to the reducer from the method. This property can have data assigned to pass to the reducer */
134 data?: any;
135 }
136 interface Rest {
137 storeProp: string;
138 path: string;
139 }
140}