Skip to content

Commit 113c142

Browse files
author
Eimantas
authored
Initial version. (#1)
1 parent 9369d38 commit 113c142

18 files changed

+6800
-1
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Don't allow people to merge changes to these generated files, because the result
2+
# may be invalid. You need to run "rush update" again.
3+
shrinkwrap.yaml merge=binary linguist-generated=true
4+
npm-shrinkwrap.json merge=binary linguist-generated=true
5+
package-lock.json merge=binary linguist-generated=true
6+
yarn.lock merge=binary linguist-generated=true
7+
**/__snapshots__/* linguist-generated=true

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
@types
3+
dist
4+
5+
junit.xml
6+
coverage
7+
.env

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"useTabs": false,
3+
"tabWidth": 4,
4+
"printWidth": 140
5+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"endregion"
4+
],
5+
"typescript.tsdk": "node_modules\\typescript\\lib"
6+
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 ReactWay
3+
Copyright (c) 2019 QuatroDev
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[![NPM version](https://img.shields.io/npm/v/@reactway/api-builder.svg?logo=npm)](https://www.npmjs.com/package/@reactway/api-builder)
2+
[![Build Status](https://img.shields.io/azure-devops/build/reactway/reactway/7/master.svg?logo=azuredevops)](https://dev.azure.com/reactway/ReactWay/_build?definitionId=7)
3+
[![Code coverage](https://img.shields.io/azure-devops/coverage/reactway/reactway/7/master.svg)](https://dev.azure.com/reactway/ReactWay/_build?definitionId=7)
4+
[![Dependencies](https://img.shields.io/david/reactway/api-builder.svg)](https://david-dm.org/reactway/api-builder)
5+
[![Dev dependencies](https://img.shields.io/david/dev/reactway/api-builder.svg)](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

Comments
 (0)