Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

Commit

Permalink
feat: add customizable axios instance & config via options
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Oct 1, 2019
1 parent d316cbe commit 5d8b87f
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 13 deletions.
186 changes: 185 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const postgrestClient = postgrester.create({
});

(async () => {
const [data, pagesLength] = postgrester
const [data, pagesLength] = postgrestClient
.select("*")
.select("author(first_name,last_name)")
.is("is_published", true)
Expand All @@ -39,6 +39,190 @@ const postgrestClient = postgrester.create({
})();
```

## API

### Options

When creating the instance via `postgrester.create()`:

| Property | Type | Default | Description |
| --------------- | -------------------- | ------- | ------------------------------------------ |
| `axiosConfig` | `AxiosRequestConfig` | `{}` | Axios config called with `axios.create()`. |
| `axiosInstance` | `AxiosInstance` | `null` | Axios custom instance. |
| `baseUri` | `string` | `""` | API URL. |

> **:warning: Important**<br>
> If you specify the `axiosInstance` property, it's useless to set the `axiosConfig` one since it
> will be overridden by your instance. And if you use the `axiosConfig` property with the `baseUri`
> one, the provided `baseUri` will ooverride the axios `baseURL` if you set it whithin your
> `axiosConfig`.
### Query Methods

#### select()

**Parameters**

| Name | Type | Default | Examples |
| ---------- | -------- | ------------ | ----------------------------------------- |
| `selector` | `string` | **required** | `"*"`, `"author"`, `"category(id,label)"` |

### Filter Methods

#### is()

**Parameters**

| Name | Type | Default | Examples |
| -------- | ---------------- | ------------ | ------------------------------ |
| `column` | `string` | **required** | `"author"`, `"category.label"` |
| `value` | `boolean | null` | **required** | |

#### eq()

**Parameters**

| Name | Type | Default | Examples |
| ------------ | ----------------- | ------------ | ------------------------------ |
| `column` | `string` | **required** | `"author"`, `"category.label"` |
| `value` | `number | string` | **required** | `"Leo Tolstoy"` |
| `withQuotes` | `boolean` | `false` | |

#### in()

**Parameters**

| Name | Type | Default | Examples |
| ------------ | ------------------------ | ------------ | -------------------------------------- |
| `column` | `string` | **required** | `"author"`, `"category.label"` |
| `value` | `Array<number | string>` | **required** | `["Leo Tolstoy", "Fyodor Dostoevsky"]` |
| `withQuotes` | `boolean` | `false` | |

#### like()

**Parameters**

| Name | Type | Default | Examples |
| -------- | -------- | ------------ | ------------------------------ |
| `column` | `string` | **required** | `"author"`, `"category.label"` |
| `value` | `string` | **required** | `"Tolstoy"` |

#### ilike()

**Parameters**

| Name | Type | Default | Examples |
| -------- | -------- | ------------ | ------------------------------ |
| `column` | `string` | **required** | `"author"`, `"category.label"` |
| `value` | `string` | **required** | `"tolstoy"` |

#### not

This getter negates the next filter method.

For example, `postgrestClient.not.is("category_id", null).ilike("author", "dostoevsky")` will negate
the `is()` filter but not the `ilike()` one.

#### and

This getter condition \*_all_ the next filter methods to be conjuncted as "ors".

For example, `postgrestClient.not.is("category_id", null).ilike("author", "dostoevsky")` will negate
the `is()` filter but not the `ilike()` one.

#### or

This getter condition \*_all_ the next filter methods to be conjuncted as "ands".

### Sort Methods

#### orderBy()

**Parameters**

| Name | Type | Default | Examples |
| -------- | --------- | ------------ | ------------------------------ |
| `column` | `string` | **required** | `"author"`, `"category.label"` |
| `isDesc` | `boolean` | `false` | |

### Pagination Methods

#### page()

**Parameters**

| Name | Type | Default | Examples |
| ----------- | -------- | ------------ | ---------- |
| `pageIndex` | `number` | **required** | `0`, `123` |
| `limit` | `number` | `10` | |

### Call Methods

All calling methods are asynchronous promises.

#### get()

**Parameters**

| Name | Type | Default | Examples |
| --------------- | --------- | ------------ | ---------- |
| path | `string` | **required** | `"/books"` |
| withPagesLength | `boolean` | `false` | |

**Return value**

```ts
Promise<{
data: any;
pagesLength: number; // -1 if the pages length was not requested or couldn't be calculated.
}>
```

#### post()

**Parameters**

| Name | Type | Default | Examples |
| --------------- | --------- | ------------ | ---------- |
| path | `string` | **required** | `"/books"` |
| withPagesLength | `boolean` | false | |

**Return value**

```ts
Promise<void>
```

#### patch()

**Parameters**

| Name | Type | Default | Examples |
| --------------- | --------- | ------------ | ---------- |
| path | `string` | **required** | `"/books"` |
| withPagesLength | `boolean` | false | |

**Return value**

```ts
Promise<void>
```

#### delete()

**Parameters**

| Name | Type | Default | Examples |
| --------------- | --------- | ------------ | ---------- |
| path | `string` | **required** | `"/books"` |
| withPagesLength | `boolean` | false | |

**Return value**

```ts
Promise<void>
```

## Contribute

### Get Started
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"preversion": "yarn test",
"postversion": "git push --tags && git push origin HEAD"
},
"dependencies": {
"axios": "0.19.0"
},
"devDependencies": {
"@types/jest": "24.0.18",
"axios": "0.19.0",
"copyfiles": "2.1.1",
"coveralls": "3.0.6",
"jest": "24.9.0",
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default {
}
],

external: ["axios"],

plugins: [
// Clean /dist directory:
cleaner({ targets: ["./dist"] }),
Expand Down
19 changes: 13 additions & 6 deletions src/Postgrester.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios, { AxiosInstance } from "axios";

import { PostgresterContructor, PostgresterInstance, PostgresterOptions } from "./types";
import { PostgresterConfig, PostgresterConstructor, PostgresterInstance } from "./types";

const Postgrester: PostgresterContructor = class Postgrester implements PostgresterInstance {
const Postgrester: PostgresterConstructor = class Postgrester implements PostgresterInstance {
private ands: string[];
private readonly axios: AxiosInstance;
private foreignSorters: { [k: string]: string[] };
Expand Down Expand Up @@ -34,10 +34,17 @@ const Postgrester: PostgresterContructor = class Postgrester implements Postgres
return this;
}

constructor({ baseUri }: PostgresterOptions) {
this.axios = axios.create({
baseURL: baseUri
});
constructor({ axiosConfig, axiosInstance, baseUri }: PostgresterConfig) {
if (axiosInstance !== null) {
this.axios = axiosInstance;
} else {
const baseAxiosConfig = axiosConfig !== null ? axiosConfig : {};

this.axios = axios.create({
...baseAxiosConfig,
baseURL: baseUri
});
}

this.reset();
}
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/Postgrester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ jest.mock("axios");
const mockedAxios = axios as jest.Mocked<typeof axios>;
mockedAxios.create.mockReturnValue(mockedAxios);

import { DEFAULT_CONFIG } from "../constants";
import Postgrester from "../Postgrester";

const postgrester = new Postgrester({
...DEFAULT_CONFIG,
baseUri: "https://contributions-api.codedutravail.num.social.gouv.fr"
});

Expand Down
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PostgresterConfig } from "./types";

export const DEFAULT_CONFIG: PostgresterConfig = {
axiosConfig: null,
axiosInstance: null,
baseUri: ""
};
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEFAULT_CONFIG } from "./constants";
// tslint:disable-next-line: import-name
import PostgresterClass from "./Postgrester";
import {
Expand All @@ -7,10 +8,6 @@ import {
PostgresterStatic
} from "./types";

const DEFAULT_CONFIG: PostgresterConfig = {
baseUri: ""
};

/**
* Create an instance of Postgrester.
*/
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/// <reference types="axios" />

import { AxiosInstance, AxiosRequestConfig } from "axios";

export type PostgresterConfig = {
axiosConfig: AxiosRequestConfig | null;
axiosInstance: AxiosInstance | null;
baseUri: string;
};
export type PostgresterOptions = Partial<PostgresterConfig>;

export interface PostgresterContructor {
export interface PostgresterConstructor {
new (options: PostgresterOptions): PostgresterInstance;
}

Expand Down

0 comments on commit 5d8b87f

Please sign in to comment.