-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c43ad2a
commit 13994e9
Showing
14 changed files
with
291 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
|
||
# [0.4.0](https://github.com/cosmology-tech/schema-typescript/compare/@interweb/node-api-client@0.3.0...@interweb/node-api-client@0.4.0) (2024-05-25) | ||
|
||
**Note:** Version bump only for package @interweb/node-api-client | ||
|
||
|
||
|
||
|
||
|
||
# 0.3.0 (2024-05-25) | ||
|
||
**Note:** Version bump only for package @interweb/node-api-client | ||
|
||
|
||
|
||
|
||
|
||
# [0.2.0](https://github.com/cosmology-tech/schema-typescript/compare/node-api-client@0.1.0...node-api-client@0.2.0) (2024-05-25) | ||
|
||
**Note:** Version bump only for package node-api-client | ||
|
||
|
||
|
||
|
||
|
||
# 0.1.0 (2024-05-25) | ||
|
||
**Note:** Version bump only for package node-api-client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# @interweb/node-api-client | ||
|
||
<p align="center"> | ||
<img src="https://user-images.githubusercontent.com/545047/188804067-28e67e5e-0214-4449-ab04-2e0c564a6885.svg" width="80"><br /> | ||
Node.js API Client | ||
</p> | ||
|
||
`@interweb/node-api-client` is a lightweight and flexible HTTP client for interacting with RESTful APIs in Node.js. It supports common HTTP methods such as GET, POST, PUT, PATCH, and DELETE, with customizable options for headers, query parameters, and timeouts. | ||
|
||
## install | ||
|
||
```sh | ||
npm install @interweb/node-api-client | ||
``` | ||
|
||
## Usage | ||
|
||
Here's an example of how to use `@interweb/node-api-client`: | ||
|
||
```js | ||
import { APIClient, APIClientOptions } from '@interweb/node-api-client'; | ||
|
||
const options: APIClientOptions = { | ||
restEndpoint: 'http://localhost:8001/api' | ||
}; | ||
|
||
const client = new APIClient(options); | ||
|
||
// GET request | ||
client.get('/endpoint') | ||
.then(response => console.log(response)) | ||
.catch(error => console.error(error)); | ||
|
||
// POST request with JSON body | ||
client.post('/endpoint', { key: 'value' }) | ||
.then(response => console.log(response)) | ||
.catch(error => console.error(error)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
it('works', () => { | ||
console.log('hello test world!'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.tsx?$': [ | ||
'ts-jest', | ||
{ | ||
babelConfig: false, | ||
tsconfig: 'tsconfig.json', | ||
}, | ||
], | ||
}, | ||
transformIgnorePatterns: [`/node_modules/*`], | ||
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$', | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
modulePathIgnorePatterns: ['dist/*'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "@interweb/fetch-api-client", | ||
"version": "0.4.0", | ||
"author": "Dan Lynch <pyramation@gmail.com>", | ||
"description": "Node.js API Client", | ||
"main": "index.js", | ||
"module": "esm/index.js", | ||
"types": "index.d.ts", | ||
"homepage": "https://github.com/cosmology-tech/schema-typescript", | ||
"license": "SEE LICENSE IN LICENSE", | ||
"publishConfig": { | ||
"access": "public", | ||
"directory": "dist" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/cosmology-tech/schema-typescript" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/cosmology-tech/schema-typescript/issues" | ||
}, | ||
"scripts": { | ||
"copy": "copyfiles -f ../../LICENSE README.md package.json dist", | ||
"clean": "rimraf dist/**", | ||
"prepare": "npm run build", | ||
"build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy", | ||
"lint": "eslint . --fix", | ||
"test": "jest", | ||
"test:watch": "jest --watch" | ||
}, | ||
"keywords": [], | ||
"gitHead": "06e20539d2cfce7522a29d7e9b20a9eb4d78fe2c", | ||
"dependencies": { | ||
"isomorphic-fetch": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/isomorphic-fetch": "^0.0.39" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import fetch from 'isomorphic-fetch'; | ||
|
||
interface RequestOptions<Params> { | ||
hostname: string; | ||
path: string; | ||
headers?: { [key: string]: string }; | ||
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; | ||
params?: Params; | ||
timeout?: number; | ||
} | ||
|
||
export interface APIClientOptions { | ||
restEndpoint: string; | ||
} | ||
|
||
export class APIClient { | ||
private baseUrl: string; | ||
private defaultTimeout: number = 10000; // 10 seconds | ||
|
||
constructor(options: APIClientOptions) { | ||
this.baseUrl = options.restEndpoint; | ||
} | ||
|
||
private buildUrl(endpoint: string, query?: { [key: string]: any }): string { | ||
const url = new URL(endpoint, this.baseUrl); | ||
if (query) { | ||
Object.keys(query).forEach(key => url.searchParams.append(key, query[key])); | ||
} | ||
return url.toString(); | ||
} | ||
|
||
private async request<Resp>(options: RequestOptions<any>): Promise<Resp> { | ||
const { path, headers, method, params, timeout } = options; | ||
const url = this.buildUrl(path, method === 'GET' && params); | ||
const fetchOptions: RequestInit = { | ||
method, | ||
headers, | ||
body: method !== 'GET' && method !== 'DELETE' ? JSON.stringify(params) : null, | ||
}; | ||
|
||
const controller = new AbortController(); | ||
const id = setTimeout(() => controller.abort(), timeout || this.defaultTimeout); | ||
fetchOptions.signal = controller.signal; | ||
|
||
const response = await fetch(url, fetchOptions); | ||
clearTimeout(id); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
return response.json() as Promise<Resp>; | ||
} | ||
|
||
get<Resp = unknown>(endpoint: string, query?: { [key: string]: any }, opts: { headers?: { [key: string]: string }, timeout?: number } = {}): Promise<Resp> { | ||
return this.request<Resp>({ | ||
path: endpoint, | ||
method: 'GET', | ||
headers: opts.headers, | ||
timeout: opts.timeout || this.defaultTimeout, | ||
params: query, // Correctly use query parameters for GET | ||
hostname: '', | ||
}); | ||
} | ||
|
||
post<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: { headers?: { [key: string]: string }, timeout?: number } = {}): Promise<Resp> { | ||
return this.request<Resp>({ | ||
path: endpoint, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...opts.headers, | ||
}, | ||
params, | ||
timeout: opts.timeout || this.defaultTimeout, | ||
hostname: '', | ||
}); | ||
} | ||
|
||
put<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: { headers?: { [key: string]: string }, timeout?: number } = {}): Promise<Resp> { | ||
return this.request<Resp>({ | ||
path: endpoint, | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...opts.headers, | ||
}, | ||
params, | ||
timeout: opts.timeout || this.defaultTimeout, | ||
hostname: '', | ||
}); | ||
} | ||
|
||
patch<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: { headers?: { [key: string]: string }, timeout?: number } = {}): Promise<Resp> { | ||
return this.request<Resp>({ | ||
path: endpoint, | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...opts.headers, | ||
}, | ||
params, | ||
timeout: opts.timeout || this.defaultTimeout, | ||
hostname: '', | ||
}); | ||
} | ||
|
||
delete<Resp = unknown>(endpoint: string, opts: { headers?: { [key: string]: string }, timeout?: number } = {}): Promise<Resp> { | ||
return this.request<Resp>({ | ||
path: endpoint, | ||
method: 'DELETE', | ||
headers: opts.headers, | ||
timeout: opts.timeout || this.defaultTimeout, | ||
hostname: '', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist/esm", | ||
"module": "es2022", | ||
"rootDir": "src/", | ||
"declaration": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src/" | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["dist", "node_modules", "**/*.spec.*", "**/*.test.*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters