9.31.0 • Published 8 months ago

@opengeoweb/api v9.31.0

Weekly downloads
-
License
Apache-2.0
Repository
gitlab
Last release
8 months ago

current version coverage

content

Table of contents generated with markdown-toc

Api

This library was generated with Nx. The api lib makes it possible to use ApiProvider to wrap your components and make the api accessible by using the useApiContext hook.

Running unit tests

Run nx test api to execute the unit tests via Jest.

Setup the api

1. Define a type for the api.

Create a file called api, and create a type for the api. This should be a type that returns promises. Example:

// api.ts
export type AppApi = {
  getInitialPresets: (
    config: ConfigType,
  ) => Promise<{ data: InitialAppPreset }>;
  getScreenPresets: (
    config: ConfigType,
  ) => Promise<{ data: WorkspacePreset[] }>;
  getBeVersion: () => Promise<{ data: { version: string } }>;
};

2. Define routes for your api.

This should be a method that returns the routes. Example:

// api.ts
const getApiRoutes = (axiosInstance: AxiosInstance): AppApi => ({
  getInitialPresets: (
    formData: FormData,
  ): Promise<{ data: InitialAppPreset }> => {
    return axiosInstance.get('/getInitialPresetsUrl', formData);
  },
  getScreenPresets: (
    formData: FormData,
  ): Promise<{ data: WorkspacePreset[] }> => {
    return axiosInstance.get('/getScreenPresets', formData);
  },
  getBeVersion: (): Promise<{ data: { version: string } }> => {
    return axiosInstance.get('/version');
  },
});

3. Create a createApi method to initiate the api

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:

// api.ts
import { createApiInstance } from '@opengeoweb/api';

export const createApi = ({
  url: string,
  appURL: string,
  auth: Credentials,
  setAuth: (cred: Credentials) => void,
  authTokenURL: string,
  authClientId: string,
}): AppApi => {
  const axiosInstance = createApiInstance(
    url,
    appURL,
    auth,
    setAuth,
    authTokenURL,
    authClientId,
  );

  return getApiRoutes(axiosInstance);
};

4. Define a baseURL inside config.json

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:

// config.json
{
   ...
  "GW_MY_NEW_TEST_API_URL": 'https://mynewapi.com/v3'
}

5. Define a fakeApi.

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:

// fakeApi.ts
export const createFakeApi = (): AppApi => {
  const api = {
    // dummy calls
    getInitialPresets: (): Promise<{ data: InitialAppPreset }> =>
      Promise.resolve({
        data: defaultInitialPresets as InitialAppPreset,
      }),
    getScreenPresets: (): Promise<{
      data: WorkspacePreset[];
    }> =>
      Promise.resolve({
        data: defaultScreenPresets as WorkspacePreset[],
      }),
    getBeVersion: (): Promise<{ data: { version: string } }> =>
      Promise.resolve({ data: { version: 'version 1.0.1' } }),
  };

  return api;
};

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:

// fakeApi.ts
import { createFakeApiInstance } from '@opengeoweb/api';
export const createApi = (): TafApi => {
  const instance = createFakeApiInstance();
  return getApiRoutes(instance);
};

How to use the api

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.

Use api inside app for production (api.ts)

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:

// app/geoweb/components/MyNewModule
  <ApiProvider
      baseURL={config.GW_MY_NEW_TEST_API_URL}
      appURL={config.GW_APP_URL}
      auth={auth}
      onSetAuth={onSetAuth}
      createApi={createApi} // coming fron api.ts
      authTokenURL={config.GW_AUTH_TOKEN_URL}
      authClientId={config.GW_AUTH_CLIENT_ID}
    >
      <ErrorBoundary>
        <MyNewModule />
      </ErrorBoundary>
    </ApiProvider>

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.

Use (fake)api inside libs for storybook

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:

// libs/myNewLib/src/lib/MyNewLibDemo.stories.tsx
  <ApiProvider
      createApi={createApi} // coming fron fakeApi.ts
    >
      <ErrorBoundary>
        <MyNewModule />
      </ErrorBoundary>
    </ApiProvider>

Use the useApiContext inside a component to access the api or fakeApi

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:

const MyComponent: React.FC = () => {
  const { api } = useApiContext<AppApi>();
  const [initialPresets, setInitialPresets] = React.useState<InitialAppPreset>([]);

  React.useEffect(()=> {
    const intitialPresets = await api.getInitialPresets(myFormvalues);
    setInitialAppPresets(intitialPresets);
  }, []);

  return <>initialPresets.map(preset => preset.name)</>
};

export const StoryBookExample = (): React.ReactElement => (
  <ApiProvider
    createApi={createApi} // coming fron fakeApi.ts
  >
      <ErrorBoundary>
        <MyComponent />
      </ErrorBoundary>
  </ApiProvider>
);
9.31.0

8 months ago

9.30.0

8 months ago

9.29.1

8 months ago

9.29.0

8 months ago

9.28.1

9 months ago

9.28.0

9 months ago

9.27.1

9 months ago

9.27.0

10 months ago

9.24.0

11 months ago

9.26.0

10 months ago

9.20.1

1 year ago

9.22.0

12 months ago

9.20.2

1 year ago

9.19.1

1 year ago

9.25.2

11 months ago

9.25.3

10 months ago

9.23.1

11 months ago

9.25.0

11 months ago

9.21.0

12 months ago

9.19.0

1 year ago

9.18.0

1 year ago

9.17.0

1 year ago

9.16.0

1 year ago

9.15.0

1 year ago

9.14.0

1 year ago

9.13.0

1 year ago

9.12.0

1 year ago

9.11.0

1 year ago

9.10.2

1 year ago

9.10.0

1 year ago

9.10.1

1 year ago

9.9.0

1 year ago

9.8.0

1 year ago

9.7.0

1 year ago

9.6.0

1 year ago

9.5.0

1 year ago

9.4.1

1 year ago

9.4.0

1 year ago

9.3.1

2 years ago

9.3.0

2 years ago

9.2.0

2 years ago

5.2.1

2 years ago

5.2.0

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

6.1.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.3

2 years ago

6.1.1

2 years ago

6.0.2

2 years ago

6.0.4

2 years ago

8.4.1

2 years ago

8.4.0

2 years ago

8.3.1

2 years ago

7.0.0

2 years ago

4.22.0

2 years ago

4.22.1

2 years ago

8.1.0

2 years ago

8.0.0

2 years ago

8.3.0

2 years ago

8.2.0

2 years ago

9.1.0

2 years ago

9.0.0

2 years ago

4.19.0

2 years ago

4.19.1

2 years ago

4.20.0

2 years ago

4.21.0

2 years ago

4.15.0

2 years ago

4.14.1

2 years ago

4.16.0

2 years ago

4.15.1

2 years ago

4.17.0

2 years ago

4.18.0

2 years ago

4.13.1

2 years ago

4.13.0

2 years ago

4.12.0

2 years ago

4.11.0

2 years ago

4.10.0

2 years ago