4.0.16 • Published 7 months ago

generic-odata-typescript-client v4.0.16

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

generic-odata-typescript-client

General usage

You supply the Odata source and resourcepath. Typically in the form of a API and a db schema + table/view and use these as part of your GOTCConfig

interface ODATARESOURCE {
  'api/v1': string;
  'api/v2': string;
}

interface ODATARESOURCEPATH {
  'Controller1': string;
  'Controller2': string;
  'Controller3': string;
}

export const configName: GOTCConfig<ODATARESOURCE, ODATARESOURCEPATH> = {
  baseURL: process.env.URL || '',
  headers: { 'X-Api-Key': YOUR_API_KEY },
  limitterOptions: { tokensPerInterval: 95, interval: 'minute' },
};

The limmitterOptions is to limit API calls to an api to X request pr interval etc. 95 request pr minutes.

interval options are: "second" | "sec" | "minute" | "min" | "hour" | "hr" | "day";
If not specified the default is: { tokensPerInterval: 1000, interval: 'minute' }

You consume it like so:

    interface Person {
      name: string;
      age: number;
    }
    
    const client = new GOTCApiClient(configName);
    await client.get<Person>({ api: 'universal', controller: 'dbo.person' });    

With querybuilder

With querybuilder:

    interface Person {
      name: string;
      age: number;
    }
    
    const client = new GOTCApiClient(configName);
    const queryBuilder = new OdataQueryBuilder<Person>();
    await client.get<Person>({ api: 'universal', controller: 'dbo.person', url: queryBuilder.select('name').toQuery()});

Filter can use: eq, ne, gt, ge, lt, le, in, startswith, endswith, contains

    interface Person {
      name: string;
      age: number;
    }
    
    const client = new GOTCApiClient(configName);
    const queryBuilder = new OdataQueryBuilder<Person>();
    await client.get<Person>({
    api: 'universal',
    controller: 'dbo.person',
    url: queryBuilder
      .filter((f) => f.eq('Name', 'Mathias').eq('age', 22))
      .select('Name', 'Age')
      .toQuery(),
  });

And is default, if you want to use or, you need to do it like this:

    interface Person {
      name: string;
      age: number;
    }
    
    const client = new GOTCApiClient(configName);
    const queryBuilder = new OdataQueryBuilder<Person>();
    await client.get<Person>({
    api: 'universal',
    controller: 'dbo.person',
    url: queryBuilder
      .filter((f) => f.or((o) => o.eq('Name', 'Jacob').eq('Name', 'Mathias'))
      .select('Name', 'Age')
      .toQuery(),
  });

All request returns a GotcResponse that can be types on the api call

    client.get<T = unknown, R = unknown>
    client.put<T = unknown, R = unknown>
    client.post<T = unknown, R = unknown>
    client.delete<T = unknown, R = unknown>

The type T will be used for the querybuilder to help filter options, if not provided there will be no help building the filters, and not needed if the filterbuilder is not used.

    const response = await flient.get<Person, Person[]>(
        'universal',
        'OPR.UniversalTableConfiguration',
        (error) => handleError(error),
        (builder) => builder.filter((filter) => filter.eq('name', 'Name')).toQuery()
      );
The type R is the type of the data value on the GotcResponse type and can be use to specify the return type from the api.
On the above response the type of the data would be Person[]
    if(response.status === 200) {
      const persons = response.data;
    }

    export interface GotcResponse<T = any, D = any>  {
      data: T;
      status: number;
      statusText: string;
      headers: AxiosResponseHeaders;
      config: AxiosRequestConfig<D>;
      request?: any;
    }

The querybuilder is under the MIT license, the rest is not.

How to publish

npm run clean
npm run build
npm publish

In theory we should add npm run docs, but we don't.

How it was build

This is created and maintained with with tsup SUUUUUP

start:

npm install --save-dev \
            @types/node \
            eslint \
            typescript \
            tsup \
            rimraf \

The scripts setup should look like:

  "scripts": {
    "build": "tsup",
    "clean": "rimraf ./dist",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

The tsup config should looklike

import { defineConfig } from "tsup";

export default defineConfig({
  entry: ["src/index.ts"],
  format: ["cjs", "esm"],
  dts: true,
  splitting: false,
  sourcemap: true,
  clean: true,
});

The TSconfig should look like:

{
  "compilerOptions": {
    "lib": [
      "dom",
      "dom.iterable",
      "ESNext"
    ],
    "esModuleInterop": true,
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "node",
    "strict": true,
    "outDir": "dist"
  }
}
4.0.5

8 months ago

4.0.4

8 months ago

4.0.7

8 months ago

4.0.6

8 months ago

4.0.9

8 months ago

4.0.8

8 months ago

4.0.10

8 months ago

4.0.16

7 months ago

4.0.15

7 months ago

4.0.12

7 months ago

4.0.11

8 months ago

4.0.14

7 months ago

4.0.13

7 months ago

4.0.1

12 months ago

4.0.3

9 months ago

4.0.2

12 months ago

3.0.27

12 months ago

3.0.23

1 year ago

3.0.24

1 year ago

3.0.25

1 year ago

3.0.26

1 year ago

3.0.21

1 year ago

3.0.22

1 year ago

3.0.20

1 year ago

3.0.19

1 year ago

3.0.16

1 year ago

3.0.17

1 year ago

3.0.15

1 year ago

3.0.18

1 year ago

3.0.14

2 years ago

3.0.12

2 years ago

3.0.10

2 years ago

3.0.11

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.9

2 years ago

2.0.8

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.8

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

2.1.9

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.8

2 years ago

2.1.7

2 years ago

3.0.9

2 years ago

2.1.0

2 years ago

1.2.9

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.2.1

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.13

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.10

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.0

2 years ago