Skip to content

Commit 03320ae

Browse files
authored
feat: Change to use typescript (#1)
1 parent 199eea5 commit 03320ae

File tree

12 files changed

+3018
-229
lines changed

12 files changed

+3018
-229
lines changed

.eslintrc.cjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
extends: ['@kapeta/eslint-config'],
3+
env: {
4+
node: true,
5+
},
6+
rules: {
7+
'@typescript-eslint/no-explicit-any': 'off',
8+
'@typescript-eslint/no-non-null-assertion': 'off',
9+
'@typescript-eslint/no-unsafe-assignment': 'off',
10+
'@typescript-eslint/no-unsafe-member-access': 'off',
11+
'@typescript-eslint/no-unsafe-return': 'off',
12+
'@typescript-eslint/restrict-plus-operands': 'off',
13+
'no-param-reassign': 'off',
14+
'@typescript-eslint/require-await': 'off',
15+
'@typescript-eslint/no-inferrable-types': 'off',
16+
'@typescript-eslint/no-misused-promises': 'off',
17+
'@typescript-eslint/no-unsafe-argument': 'off',
18+
},
19+
parserOptions: {
20+
project: `${__dirname}/tsconfig.json`,
21+
tsconfigRootDir: __dirname,
22+
},
23+
};

.github/workflows/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Main build
2+
on:
3+
pull_request:
4+
push:
5+
branches: ['master']
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-node@v3
13+
- run: npm ci
14+
- run: npm run build
15+
# Probably move this to its own job when it makes sense
16+
- name: Semantic Release
17+
uses: cycjimmy/semantic-release-action@v3
18+
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
21+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/publish.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,3 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
lerna-debug.log*
8-
9-
# Diagnostic reports (https://nodejs.org/api/report.html)
10-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11-
12-
# Runtime data
13-
pids
14-
*.pid
15-
*.seed
16-
*.pid.lock
17-
18-
# Directory for instrumented libs generated by jscoverage/JSCover
19-
lib-cov
20-
21-
# Coverage directory used by tools like istanbul
22-
coverage
23-
*.lcov
24-
25-
# nyc test coverage
26-
.nyc_output
27-
28-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29-
.grunt
30-
31-
# Bower dependency directory (https://bower.io/)
32-
bower_components
33-
34-
# node-waf configuration
35-
.lock-wscript
36-
37-
# Compiled binary addons (https://nodejs.org/api/addons.html)
38-
build/Release
39-
40-
# Dependency directories
41-
node_modules/
42-
jspm_packages/
43-
44-
# TypeScript v1 declaration files
45-
typings/
46-
47-
# TypeScript cache
48-
*.tsbuildinfo
49-
50-
# Optional npm cache directory
51-
.npm
52-
53-
# Optional eslint cache
54-
.eslintcache
55-
56-
# Microbundle cache
57-
.rpt2_cache/
58-
.rts2_cache_cjs/
59-
.rts2_cache_es/
60-
.rts2_cache_umd/
61-
62-
# Optional REPL history
63-
.node_repl_history
64-
65-
# Output of 'npm pack'
66-
*.tgz
67-
68-
# Yarn Integrity file
69-
.yarn-integrity
70-
71-
# dotenv environment variables file
72-
.env
73-
.env.test
74-
75-
# parcel-bundler cache (https://parceljs.org/)
76-
.cache
77-
78-
# Next.js build output
79-
.next
80-
81-
# Nuxt.js build / generate output
82-
.nuxt
83-
dist
84-
85-
# Gatsby files
86-
.cache/
87-
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88-
# https://nextjs.org/blog/next-9-1#public-directory-support
89-
# public
90-
91-
# vuepress build output
92-
.vuepress/dist
93-
94-
# Serverless directories
95-
.serverless/
96-
97-
# FuseBox cache
98-
.fusebox/
99-
100-
# DynamoDB Local files
101-
.dynamodb/
102-
103-
# TernJS port file
104-
.tern-port
105-
106-
.DS_Store
1+
node_modules
2+
.DS_Store
3+
dist/*

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.iml

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/*

index.d.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

index.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
type RequestArgumentTransport = 'path' | 'header' | 'body' | 'query';
2+
type RequestMethod =
3+
| 'GET'
4+
| 'POST'
5+
| 'DELETE'
6+
| 'PATCH'
7+
| 'PUT'
8+
| 'OPTIONS'
9+
| 'HEAD'
10+
| 'TRACE'
11+
| 'CONNECT'
12+
| 'LINK'
13+
| 'UNLINK'
14+
| 'COPY'
15+
| 'PURGE'
16+
| 'LOCK'
17+
| 'UNLOCK'
18+
| 'PROPFIND'
19+
| 'VIEW';
20+
export interface RequestArgument {
21+
name: string;
22+
value: any;
23+
transport: RequestArgumentTransport;
24+
}
25+
26+
export class RestClient {
27+
private readonly _baseUrl: string;
28+
29+
/**
30+
* Initialise rest client
31+
*
32+
* @param {string} baseUrl
33+
*/
34+
constructor(baseUrl: string) {
35+
if (!baseUrl) {
36+
baseUrl = '/';
37+
}
38+
39+
if (!baseUrl.endsWith('/')) {
40+
baseUrl += '/';
41+
}
42+
43+
this._baseUrl = baseUrl;
44+
}
45+
46+
/**
47+
*
48+
* @param {string} method
49+
* @param {string} path
50+
* @param {RequestArgument[]} requestArguments
51+
* @return {Promise<Object>}
52+
*/
53+
async execute(method: RequestMethod, path: string, requestArguments: RequestArgument[] = []) {
54+
while (path.startsWith('/')) {
55+
path = path.substring(1);
56+
}
57+
58+
let url = this._baseUrl + path;
59+
60+
const query: string[] = [];
61+
const headers: { [key: string]: string } = {
62+
accept: 'application/json',
63+
};
64+
const opts: RequestInit = {
65+
method,
66+
headers,
67+
};
68+
69+
requestArguments.forEach((requestArgument) => {
70+
switch (requestArgument.transport.toLowerCase()) {
71+
case 'path':
72+
url = url.replaceAll('{' + requestArgument.name + '}', requestArgument.value);
73+
break;
74+
case 'header':
75+
headers[requestArgument.name] = requestArgument.value;
76+
break;
77+
case 'body':
78+
if (!headers['content-type']) {
79+
headers['content-type'] = 'application/json';
80+
}
81+
opts.body = JSON.stringify(requestArgument.value);
82+
break;
83+
case 'query':
84+
query.push(
85+
encodeURIComponent(requestArgument.name) + '=' + encodeURIComponent(requestArgument.value)
86+
);
87+
break;
88+
default:
89+
throw new Error('Unknown argument transport: ' + requestArgument.transport);
90+
}
91+
});
92+
93+
if (query.length > 0) {
94+
url += '?' + query.join('&');
95+
}
96+
97+
const result = await fetch(url, opts);
98+
99+
return result.json();
100+
}
101+
}

0 commit comments

Comments
 (0)