-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
8 changed files
with
141 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"arrowParens": "always", | ||
"trailingComma": "none", | ||
"semi": true, | ||
"singleQuote": true, | ||
"printWidth": 120, | ||
"endOfLine": "lf", | ||
"tabWidth": 4 | ||
} |
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 |
---|---|---|
|
@@ -2,14 +2,17 @@ | |
"name": "nxrouter", | ||
"packageManager": "[email protected]", | ||
"description": "Simple, functional http-client router for JavaScript/TypeScript", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"files": ["dist"], | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsup", | ||
"test": "node ./test/index.mjs" | ||
"test": "node ./test/index.mjs", | ||
"format": "prettier --write \"./{src,test}/**/*.{js,ts,mjs,mts}\"" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
|
@@ -33,6 +36,7 @@ | |
}, | ||
"homepage": "https://github.com/neplextech/nxrouter#readme", | ||
"devDependencies": { | ||
"prettier": "^2.8.8", | ||
"tsup": "^6.7.0", | ||
"typescript": "^5.0.4" | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { NxRouter, MethodImplementor, ParamArgs } from '../src/index'; | ||
|
||
// base url endpoint | ||
const BASE = 'https://my-json-server.typicode.com/typicode'; | ||
|
||
interface ApiRoutes { | ||
demo: { | ||
posts: MethodImplementor<{ | ||
(id: ParamArgs): MethodImplementor; | ||
}>; | ||
}; | ||
} | ||
|
||
// nxrouter | ||
const client = new NxRouter<ApiRoutes>({ | ||
// request implementor | ||
async onRequest(options) { | ||
console.log(`Requesting ${options.path}`); | ||
|
||
// here we make request using fetch api | ||
const res = await fetch(`${BASE}${options.path}`, { | ||
method: options.method, | ||
...options.data | ||
}); | ||
|
||
if (!res.ok) throw new Error(`Failed with status code ${res.status}`); | ||
|
||
// and return json response | ||
return await res.json(); | ||
} | ||
}); | ||
|
||
interface APIResponse { | ||
id: number; | ||
title: string; | ||
} | ||
|
||
// initiate GET /demo/posts | ||
console.log(await client.api.demo.posts.get<APIResponse[]>()); | ||
|
||
// initiate GET /demo/posts/1 | ||
console.log(await client.api.demo.posts(1).get<APIResponse>()); |
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