@opengeoweb/api 4.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,220 @@
1
+ ![current version](https://img.shields.io/badge/dynamic/json?color=blue&label=version&query=version&url=https://gitlab.com/opengeoweb/opengeoweb/raw/master/libs/api/package.json)
2
+ ![coverage](https://gitlab.com/opengeoweb/opengeoweb/badges/master/coverage.svg?job=test-api)
3
+
4
+ # content
5
+
6
+ - [api](#api)
7
+ - [Running unit tests](#running-unit-tests)
8
+ - [Setup the api](#setup-the-api)
9
+ - [1. Define a type for the api.](#1-define-a-type-for-the-api)
10
+ - [2. Define routes for your api.](#2-define-routes-for-your-api)
11
+ - [3. Create a createApi method to initiate the api](#3-create-a-createapi-method-to-initiate-the-api)
12
+ - [4. Define a baseURL inside config.json](#4-define-a-baseurl-inside-configjson)
13
+ - [5. Define a `fakeApi`.](#5-define-a--fakeapi-)
14
+ - [How to use the api](#how-to-use-the-api)
15
+ - [Use api inside app for production (api.ts)](#use-api-inside-app-for-production--apits-)
16
+ - [Use (fake)api inside libs for storybook](#use--fake-api-inside-libs-for-storybook)
17
+ - [Use the `useApiContext` to access the api or fakeApi](#use-the--useapicontext--to-access-the-api-or-fakeapi)
18
+
19
+ <small><i><a href='http://ecotrust-canada.github.io/markdown-toc/'>Table of contents generated with markdown-toc</a></i></small>
20
+
21
+ # Api
22
+
23
+ This library was generated with [Nx](https://nx.dev). The api lib makes it possible to use `ApiProvider` to wrap your components and make the `api` accessible by using the `useApiContext` hook.
24
+
25
+ ## Running unit tests
26
+
27
+ Run `nx test api` to execute the unit tests via [Jest](https://jestjs.io).
28
+
29
+ ## Setup the api
30
+
31
+ ### 1. Define a type for the api.
32
+
33
+ Create a file called `api`, and create a type for the api. This should be a type that returns promises. Example:
34
+
35
+ ```
36
+ // api.ts
37
+ export type AppApi = {
38
+ getInitialPresets: (
39
+ config: ConfigType,
40
+ ) => Promise<{ data: InitialAppPreset }>;
41
+ getScreenPresets: (
42
+ config: ConfigType,
43
+ ) => Promise<{ data: ScreenManagerPreset[] }>;
44
+ getBeVersion: () => Promise<{ data: { version: string } }>;
45
+ };
46
+ ```
47
+
48
+ ### 2. Define routes for your api.
49
+
50
+ This should be a method that returns the routes. Example:
51
+
52
+ ```
53
+ // api.ts
54
+ const getApiRoutes = (axiosInstance: AxiosInstance): AppApi => ({
55
+ getInitialPresets: (
56
+ formData: FormData,
57
+ ): Promise<{ data: InitialAppPreset }> => {
58
+ return axiosInstance.get('/getInitialPresetsUrl', formData);
59
+ },
60
+ getScreenPresets: (
61
+ formData: FormData,
62
+ ): Promise<{ data: ScreenManagerPreset[] }> => {
63
+ return axiosInstance.get('/getScreenPresets', formData);
64
+ },
65
+ getBeVersion: (): Promise<{ data: { version: string } }> => {
66
+ return axiosInstance.get('/version');
67
+ },
68
+ });
69
+ ```
70
+
71
+ ### 3. Create a createApi method to initiate the api
72
+
73
+ At GeoWeb we use `axios` to do request to apis. Since we also need authentication, we can't use our defined api directly, so create a small helper method to combine the axiosInstance with our api:
74
+
75
+ ```
76
+ // api.ts
77
+ import { createApiInstance } from '@opengeoweb/api';
78
+
79
+ export const createApi = ({
80
+ url: string,
81
+ appURL: string,
82
+ auth: Credentials,
83
+ setAuth: (cred: Credentials) => void,
84
+ authTokenURL: string,
85
+ authClientId: string,
86
+ }): AppApi => {
87
+ const axiosInstance = createApiInstance(
88
+ url,
89
+ appURL,
90
+ auth,
91
+ setAuth,
92
+ authTokenURL,
93
+ authClientId,
94
+ );
95
+
96
+ return getApiRoutes(axiosInstance);
97
+ };
98
+
99
+ ```
100
+
101
+ ### 4. Define a baseURL inside config.json
102
+
103
+ Your api needs a baseURL in order to work. Add this key to the config.json. Be aware, this involves more changes in other repositories. Example:
104
+
105
+ ```
106
+ // config.json
107
+ {
108
+ ...
109
+ "GW_MY_NEW_TEST_API_URL": 'https://mynewapi.com/v3'
110
+ }
111
+ ```
112
+
113
+ ### 5. Define a `fakeApi`.
114
+
115
+ Since storybook can't handle authenthication we need to mock the api with dummy data locally. It's also nice for testing functionality out without to have the geoweb app running. Create a new file called `fakeApi`, and add the same apiRoutes but let it return dummy data. Example:
116
+
117
+ ```
118
+ // fakeApi.ts
119
+ export const createFakeApi = (): AppApi => {
120
+ const api = {
121
+ // dummy calls
122
+ getInitialPresets: (): Promise<{ data: InitialAppPreset }> =>
123
+ Promise.resolve({
124
+ data: defaultInitialPresets as InitialAppPreset,
125
+ }),
126
+ getScreenPresets: (): Promise<{
127
+ data: ScreenManagerPreset[];
128
+ }> =>
129
+ Promise.resolve({
130
+ data: defaultScreenPresets as ScreenManagerPreset[],
131
+ }),
132
+ getBeVersion: (): Promise<{ data: { version: string } }> =>
133
+ Promise.resolve({ data: { version: 'version 1.0.1' } }),
134
+ };
135
+
136
+ return api;
137
+ };
138
+ ```
139
+
140
+ Also create a `createApi` method. Since we're not doing real requests with axios with this, we won't need it and use the `fakeAxiosInstance`. Example:
141
+
142
+ ```
143
+ // fakeApi.ts
144
+ import { createFakeApiInstance } from '@opengeoweb/api';
145
+ export const createApi = (): TafApi => {
146
+ const instance = createFakeApiInstance();
147
+ return getApiRoutes(instance);
148
+ };
149
+ ```
150
+
151
+ ## How to use the api
152
+
153
+ If you follow the steps above, you end up with 2 new files (api.ts and fakeApi.ts) and a new config key for the json.
154
+
155
+ #### Use api inside app for production (api.ts)
156
+
157
+ If you want to try the api out with real data, you should do this inside the `app`. First, wrap the component which needs the api with the ApiProvider, and pass the `createApi` method from the api:
158
+
159
+ ```
160
+ // app/geoweb/components/MyNewModule
161
+ <ApiProvider
162
+ baseURL={config.GW_MY_NEW_TEST_API_URL}
163
+ appURL={config.GW_APP_URL}
164
+ auth={auth}
165
+ onSetAuth={onSetAuth}
166
+ createApi={createApi} // coming fron api.ts
167
+ authTokenURL={config.GW_AUTH_TOKEN_URL}
168
+ authClientId={config.GW_AUTH_CLIENT_ID}
169
+ >
170
+ <ErrorBoundary>
171
+ <MyNewModule />
172
+ </ErrorBoundary>
173
+ </ApiProvider>
174
+ ```
175
+
176
+ The `baseURL` should get the url you've defined earlier. The `createApi` should pass the `createApi` method we've defined. The rest of the props you can pass from the props of geoweb.
177
+
178
+ #### Use (fake)api inside libs for storybook
179
+
180
+ If you want to try the api out with fake data, you should do this inside storybook. First, wrap the component which needs the api with the ApiProvider. and pass the `createApi` method from the fakeApi:
181
+
182
+ ```
183
+ // libs/myNewLib/src/lib/MyNewLibDemo.stories.tsx
184
+ <ApiProvider
185
+ createApi={createApi} // coming fron fakeApi.ts
186
+ >
187
+ <ErrorBoundary>
188
+ <MyNewModule />
189
+ </ErrorBoundary>
190
+ </ApiProvider>
191
+ ```
192
+
193
+ #### Use the `useApiContext` inside a component to access the api or fakeApi
194
+
195
+ Inside your component you can use the `useApiContext` to access the defined routes. Please note that the `ApiProvider` needs to be used around one of the parent components for this to work. Any story or tests should therefore also be wrapped:
196
+
197
+ ```
198
+ const MyComponent: React.FC = () => {
199
+ const { api } = useApiContext<AppApi>();
200
+ const [initialPresets, setInitialPresets] = React.useState<InitialAppPreset>([]);
201
+
202
+ React.useEffect(()=> {
203
+ const intitialPresets = await api.getInitialPresets(myFormvalues);
204
+ setInitialAppPresets(intitialPresets);
205
+ }, []);
206
+
207
+ return <>initialPresets.map(preset => preset.name)</>
208
+ };
209
+
210
+ export const StoryBookExample = (): React.ReactElement => (
211
+ <ApiProvider
212
+ createApi={createApi} // coming fron fakeApi.ts
213
+ >
214
+ <ErrorBoundary>
215
+ <MyComponent />
216
+ </ErrorBoundary>
217
+ </ApiProvider>
218
+ );
219
+
220
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './lib/components/apihooks';
2
+ export * from './lib/components/ApiContext';