|
| 1 | +[](https://www.npmjs.com/package/@reactway/api-builder) |
| 2 | +[](https://dev.azure.com/reactway/ReactWay/_build?definitionId=7) |
| 3 | +[](https://dev.azure.com/reactway/ReactWay/_build?definitionId=7) |
| 4 | +[](https://david-dm.org/reactway/api-builder) |
| 5 | +[](https://david-dm.org/reactway/api-builder?type=dev) |
| 6 | + |
| 7 | +# @reactway/api-builder |
| 8 | + |
| 9 | +An easy api client builder for applications with identity. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- API builder is ready for SPA development |
| 14 | +- OAuth identity mechanism included |
| 15 | + |
| 16 | +## Get started |
| 17 | + |
| 18 | +```sh |
| 19 | +$ npm install @reactway/api-builder |
| 20 | +``` |
| 21 | + |
| 22 | +To start using api builder first it needs to make an `ApiConfiguration` with structure (**host** field is **required**). Then initiate class with created configuration. |
| 23 | + |
| 24 | +```ts |
| 25 | +interface ApiConfiguration { |
| 26 | + host: string; |
| 27 | + path?: string; |
| 28 | + defaultHeaders?: { [index: string]: string }; |
| 29 | + defaultAuthRequired?: boolean; |
| 30 | + identity?: IdentityMechanism; |
| 31 | + defaultQueryParams?: QueryParams; |
| 32 | + requestQueueLimit?: number; |
| 33 | +} |
| 34 | + |
| 35 | +const apiConfiguration: ApiConfiguration = { |
| 36 | + host: "https://example.com" |
| 37 | +}; |
| 38 | + |
| 39 | +const ApiClient = new ApiBuilder(apiConfiguration); |
| 40 | +``` |
| 41 | + |
| 42 | +To make request you have create an `ApiRequest` typed object or arrow function if you want to pass parameters. `method` and `requestPath` fields are **required** for `ApiRequest`. |
| 43 | + |
| 44 | +```ts |
| 45 | +const getExample: ApiRequest = { |
| 46 | + method: HttpMethods.GET |
| 47 | + requestPath: PATH_GET |
| 48 | +}; |
| 49 | + |
| 50 | +const getExample = (id: number): ApiRequest => { |
| 51 | + return { |
| 52 | + method: HttpMethods.GET, |
| 53 | + requestPath: `/${id}` |
| 54 | + }; |
| 55 | +}; |
| 56 | +``` |
| 57 | + |
| 58 | +Only http(s) method `GET` does not take `body` param. Rest of methods takes passed typed `body`. To pass typed `body` to a request here is an example. |
| 59 | + |
| 60 | +```ts |
| 61 | +interface Person { |
| 62 | + name: string; |
| 63 | + surname: string; |
| 64 | +} |
| 65 | + |
| 66 | +const getExample = await apiBuilder.post<Person>({ |
| 67 | + requestPath: "/post", |
| 68 | + body: { |
| 69 | + name: "John", |
| 70 | + surname: "Snow" |
| 71 | + } |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +Also you can pass `QueryParams`. |
| 76 | + |
| 77 | +```ts |
| 78 | +type QueryParams = { [key: string]: string | number | Array<string | number> }; |
| 79 | + |
| 80 | +const getExample: ApiRequest = { |
| 81 | + method: HttpMethods.GET |
| 82 | + requestPath: PATH_GET, |
| 83 | + queryParams: { |
| 84 | + page: 2 |
| 85 | + } |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +## API |
| 90 | + |
| 91 | +WIP |
| 92 | + |
| 93 | +## Documentation |
| 94 | + |
| 95 | +WIP |
| 96 | + |
| 97 | +## License |
| 98 | + |
| 99 | +Released under the [MIT license](LICENSE). |
0 commit comments