Skip to content

Commit 90a4cb2

Browse files
committed
feat: add new helper to create api with default settings and add testing
1 parent 7d1bd34 commit 90a4cb2

11 files changed

+489
-70
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TEST_GITEA_TOKEN=

.github/workflows/tests.yml

+61-55
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,77 @@ on:
88
- main
99

1010
jobs:
11-
# unit-tests:
12-
# name: Unit tests
13-
# runs-on: ubuntu-latest
14-
# steps:
15-
# - name: Checkout
16-
# uses: actions/checkout@v2
17-
# with:
18-
# fetch-depth: 0
11+
unit-tests:
12+
name: Unit tests
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
with:
18+
fetch-depth: 0
1919

20-
# - name: Setup Node.js
21-
# uses: actions/setup-node@v2
22-
# with:
23-
# node-version: '14'
20+
- name: Setup Node.js
21+
uses: actions/setup-node@v2
22+
with:
23+
node-version: '14'
2424

25-
# - name: Cache pnpm modules
26-
# uses: actions/cache@v2
27-
# with:
28-
# path: ~/.pnpm-store
29-
# key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
30-
# restore-keys: |
31-
# ${{ runner.os }}-
25+
- name: Cache pnpm modules
26+
uses: actions/cache@v2
27+
with:
28+
path: ~/.pnpm-store
29+
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
30+
restore-keys: |
31+
${{ runner.os }}-
3232
33-
# - uses: pnpm/[email protected]
34-
# with:
35-
# version: 6
36-
# run_install: true
33+
- uses: pnpm/[email protected]
34+
with:
35+
version: 6
36+
run_install: true
3737

38-
# - name: Unit test
39-
# run: pnpm run test
38+
- name: Generate api
39+
run: pnpm generate:api
4040

41-
# - uses: artiomtr/[email protected]
42-
# if: "github.event_name == 'pull_request'"
43-
# with:
44-
# github-token: ${{ secrets.GITHUB_TOKEN }}
45-
# skip-step: all
41+
- name: Unit test
42+
run: pnpm coverage
4643

47-
# typecheck:
48-
# name: Typecheck
49-
# runs-on: ubuntu-latest
50-
# steps:
51-
# - name: Checkout
52-
# uses: actions/checkout@v2
53-
# with:
54-
# fetch-depth: 0
44+
# - uses: artiomtr/[email protected]
45+
# if: "github.event_name == 'pull_request'"
46+
# with:
47+
# github-token: ${{ secrets.GITHUB_TOKEN }}
48+
# skip-step: all
5549

56-
# - name: Setup Node.js
57-
# uses: actions/setup-node@v2
58-
# with:
59-
# node-version: '14'
50+
typecheck:
51+
name: Typecheck
52+
runs-on: ubuntu-latest
53+
steps:
54+
- name: Checkout
55+
uses: actions/checkout@v2
56+
with:
57+
fetch-depth: 0
6058

61-
# - name: Cache pnpm modules
62-
# uses: actions/cache@v2
63-
# with:
64-
# path: ~/.pnpm-store
65-
# key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
66-
# restore-keys: |
67-
# ${{ runner.os }}-
59+
- name: Setup Node.js
60+
uses: actions/setup-node@v2
61+
with:
62+
node-version: '14'
6863

69-
# - uses: pnpm/[email protected]
70-
# with:
71-
# version: 6
72-
# run_install: true
64+
- name: Cache pnpm modules
65+
uses: actions/cache@v2
66+
with:
67+
path: ~/.pnpm-store
68+
key: ${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
69+
restore-keys: |
70+
${{ runner.os }}-
71+
72+
- uses: pnpm/[email protected]
73+
with:
74+
version: 6
75+
run_install: true
76+
77+
- name: Generate api
78+
run: pnpm generate:api
7379

74-
# - name: Typecheck
75-
# run: pnpm run typecheck
80+
- name: Typecheck
81+
run: pnpm typecheck
7682

7783
# lint:
7884
# name: Lint

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
node_modules
22
dist/
33
docs/
4-
src/*
5-
!src/.gitkeep
64
coverage/
75
junit.xml
86
*.tgz
9-
report.json
7+
report.json
8+
.env
9+
src/api.ts

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ dist/
22
coverage/
33
pnpm-lock.yaml
44
report.json
5-
src/
5+
src/api.ts
66
docs/

README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,32 @@
88

99
Gitea-js is an api client created based on the official [swagger definition](https://gitea.com/swagger.v1.json) from Gitea. Generated client uses [Fetch Api](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) (native browser support) to make requests.
1010

11-
## Exsamples
11+
## Examples
1212

1313
### Browser
1414

1515
```ts
1616
import { Api } from 'gitea-js';
1717

18-
const api = new Api({
19-
baseUrl: 'https://try.gitea.com/',
18+
const api = new giteaApi('https://try.gitea.com/', {
19+
token: 'access-token', // generate one at https://gitea.example.com/user/settings/applications
2020
});
2121

22-
const api = new Api();
2322
const repo = api.repos.repoGet('anbraten', 'gitea-js');
2423
console.log(repo);
2524
```
2625

2726
### Node.js
2827

2928
```js
30-
const { Api } = require('gitea-js');
31-
const fetch = require('node-fetch'); // You have to use node-fetch dependency for Node.JS
29+
const { createApi } = require('gitea-js');
30+
const fetch = require('cross-fetch'); // You have to use a fetch compatible polyfill like cross-fetch for Node.JS
3231

33-
const api = new Api({
34-
baseUrl: 'https://try.gitea.com/',
32+
const api = new giteaApi('https://try.gitea.com/', {
33+
token: 'access-token', // generate one at https://gitea.example.com/user/settings/applications
34+
customFetch: fetch,
3535
});
3636

37-
const api = new Api({ customFetch: fetch });
3837
const repo = api.repos.repoGet('anbraten', 'gitea-js');
3938
console.log(repo);
4039
```

package.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,33 @@
1919
],
2020
"scripts": {
2121
"download": "wget https://gitea.com/swagger.v1.json -O ./src/swagger.json",
22-
"generate:api": "swagger-typescript-api -p ./src/swagger.json -o ./src -n index.ts",
22+
"generate:api": "swagger-typescript-api -p https://gitea.com/swagger.v1.json -o ./src -n api.ts",
2323
"generate:docs": "typedoc ./src/index.ts",
2424
"generate": "pnpm generate:api && pnpm generate:docs",
2525
"build": "tsup src/index.ts --dts --format cjs,esm",
2626
"all": "pnpm download && pnpm generate && pnpm build",
2727
"clean": "rm -rf dist/ node_modules/",
28+
"test": "dotenv vitest",
29+
"test:ui": "pnpm test -- --ui",
30+
"coverage": "pnpm test run -- --coverage",
2831
"lint:format": "prettier --check .",
2932
"typecheck": "tsc --noEmit",
3033
"release": "semantic-release"
3134
},
3235
"devDependencies": {
3336
"@geprog/semantic-release-config": "1.0.0",
3437
"@types/node": "17.0.13",
38+
"@vitest/ui": "0.5.9",
39+
"c8": "7.11.0",
40+
"cross-fetch": "3.1.5",
41+
"dotenv-cli": "5.0.0",
3542
"prettier": "2.5.1",
3643
"semantic-release": "19.0.2",
3744
"swagger-typescript-api": "9.3.1",
3845
"tsup": "5.11.13",
3946
"typedoc": "0.22.11",
40-
"typescript": "4.5.5"
47+
"typescript": "4.5.5",
48+
"vite": "2.8.6",
49+
"vitest": "0.5.9"
4150
}
4251
}

0 commit comments

Comments
 (0)