mirror of
https://codeberg.org/reflect/reflect-client-js.git
synced 2025-09-14 20:03:40 +02:00
Compare commits
No commits in common. "master" and "1.0.0" have entirely different histories.
5 changed files with 11 additions and 144 deletions
52
.npmignore
52
.npmignore
|
@ -1,52 +0,0 @@
|
||||||
/src
|
|
||||||
|
|
||||||
# Bootstrapping #
|
|
||||||
#################
|
|
||||||
/node_modules
|
|
||||||
/public/hot
|
|
||||||
/public/storage
|
|
||||||
/storage/*.key
|
|
||||||
/vendor
|
|
||||||
.env
|
|
||||||
.env.ini
|
|
||||||
.env.backup
|
|
||||||
.phpunit.result.cache
|
|
||||||
Homestead.json
|
|
||||||
Homestead.yaml
|
|
||||||
npm-debug.log
|
|
||||||
yarn-error.log
|
|
||||||
public/robots.txt
|
|
||||||
|
|
||||||
# OS generated files #
|
|
||||||
######################
|
|
||||||
.DS_Store
|
|
||||||
.DS_Store?
|
|
||||||
._*
|
|
||||||
.Spotlight-V100
|
|
||||||
.Trashes
|
|
||||||
Icon?
|
|
||||||
ehthumbs.db
|
|
||||||
Thumbs.db
|
|
||||||
.directory
|
|
||||||
|
|
||||||
# Tool specific files #
|
|
||||||
#######################
|
|
||||||
# vim
|
|
||||||
*~
|
|
||||||
*.swp
|
|
||||||
*.swo
|
|
||||||
# sublime text & textmate
|
|
||||||
*.sublime-*
|
|
||||||
*.stTheme.cache
|
|
||||||
*.tmlanguage.cache
|
|
||||||
*.tmPreferences.cache
|
|
||||||
# Eclipse
|
|
||||||
.settings/*
|
|
||||||
# JetBrains, aka PHPStorm, IntelliJ IDEA
|
|
||||||
.idea/*
|
|
||||||
# NetBeans
|
|
||||||
nbproject/*
|
|
||||||
# Visual Studio Code
|
|
||||||
.vscode
|
|
||||||
# Sass preprocessor
|
|
||||||
.sass-cache/
|
|
56
README.md
56
README.md
|
@ -1,56 +0,0 @@
|
||||||
# Reflect API client for JavaScript
|
|
||||||
|
|
||||||
Make requests to an API built using the [Reflect API](https://github.com/VictorWesterlund/reflect) framework over HTTP with JavaScript.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
```js
|
|
||||||
import { Client, Method } from "./reflect-client/Reflect.js
|
|
||||||
|
|
||||||
const api = new Client("<api_url>", "<optional_api_key");
|
|
||||||
// Make an API request with Client.call() which returns a normal JavaScript Response object
|
|
||||||
api.call("my/endpoint", Method.GET); // obj<Respone>
|
|
||||||
```
|
|
||||||
|
|
||||||
## Supported browsers/environments:
|
|
||||||
<br>Chrome|<br>Firefox|<br>Safari|<br>NodeJS|<br>Deno
|
|
||||||
--|--|--|--|--
|
|
||||||
✅ 80+|✅ 75+|✅ 14.1+|✅ 20.7.0+|✅ 1.37.0+
|
|
||||||
|
|
||||||
## How to use
|
|
||||||
|
|
||||||
1. **Install with npm**
|
|
||||||
|
|
||||||
```
|
|
||||||
npm i reflect-client
|
|
||||||
```
|
|
||||||
|
|
||||||
2. **Initialize the class**
|
|
||||||
|
|
||||||
```js
|
|
||||||
import { Client, Method } from "./reflect-client/Reflect.js
|
|
||||||
|
|
||||||
const api = new Client("<api_url>", "<optional_api_key");
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **Make API request**
|
|
||||||
|
|
||||||
Use the `call()` method to perform a request
|
|
||||||
|
|
||||||
```js
|
|
||||||
api.call("my/endpoint", Method.GET);
|
|
||||||
```
|
|
||||||
|
|
||||||
Argument index|Type|Required|Description
|
|
||||||
--|--|--|--
|
|
||||||
0|string|Yes|Fully qualified pathname and query params of the endpoint to call
|
|
||||||
1|Method\|string|Yes|A supported [Reflect HTTP method](https://github.com/VictorWesterlund/reflect/wiki/Supported-technologies#http-request-methods) (eg. `Method.GET`) or a string represnetation of a supported method (eg. "GET")
|
|
||||||
2|array|No|An optional indexed, associative, or multidimensional array that will be sent as the request body as `Content-Type: application/json`
|
|
||||||
|
|
||||||
The `call()` function will return a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object
|
|
||||||
|
|
||||||
# Development
|
|
||||||
|
|
||||||
NodeJS required version: 20.7.0+
|
|
||||||
|
|
||||||
TypeScript required version: 5.2.2+
|
|
11
package.json
11
package.json
|
@ -1,16 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "reflect-client",
|
"name": "reflect-client",
|
||||||
"version": "1.1.2",
|
"version": "1.0.0",
|
||||||
"main": "build/Reflect.js",
|
"main": "build/Reflect.js",
|
||||||
"type": "module",
|
|
||||||
"exports": {
|
|
||||||
".": {
|
|
||||||
"import": {
|
|
||||||
"types": "./src/Reflect.ts",
|
|
||||||
"default": "./build/Reflect.js"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"typescript": "^5.2.2"
|
"typescript": "^5.2.2"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
import { Method } from "./Method.js";
|
import { Method } from "./Method";
|
||||||
|
|
||||||
// Export Method as importable directly from the Reflect.js ESM
|
|
||||||
export { Method };
|
|
||||||
|
|
||||||
// Connect to a Reflect API instance over HTTP
|
// Connect to a Reflect API instance over HTTP
|
||||||
export default class Client {
|
export default class Client {
|
||||||
|
@ -9,8 +6,7 @@ export default class Client {
|
||||||
private headers: object = {};
|
private headers: object = {};
|
||||||
|
|
||||||
constructor(url: string, key: string|null = null) {
|
constructor(url: string, key: string|null = null) {
|
||||||
// Append tailing slash if omitted from URL string
|
this.url = url;
|
||||||
this.url = url.substring(url.length - 1) === "/" ? url : url + "/";
|
|
||||||
|
|
||||||
// Use API key with requests if defined
|
// Use API key with requests if defined
|
||||||
if (key) {
|
if (key) {
|
||||||
|
@ -18,22 +14,9 @@ export default class Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set request header
|
|
||||||
private setHeader(name, value): void {
|
|
||||||
return this.headers[name] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set API key to use for all requests
|
// Set API key to use for all requests
|
||||||
private setApiKey(key: string): void {
|
private setApiKey(key: string): void {
|
||||||
return this.setHeader("Authorization", `Bearer ${key}`);
|
this.headers["Authentication"] = `Bearer ${key}`;
|
||||||
}
|
|
||||||
|
|
||||||
// Get fully qualified URL to endpoint
|
|
||||||
private getEndpoint(endpoint: string): string {
|
|
||||||
// Remove leading slash if provided for pathname. It's already set on the domain name
|
|
||||||
endpoint = endpoint.substring(0, 1) !== "/" ? endpoint : endpoint.substring(1, endpoint.length);
|
|
||||||
|
|
||||||
return this.url + endpoint;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call a Reflect API endpoint with method and optional payload
|
// Call a Reflect API endpoint with method and optional payload
|
||||||
|
@ -43,13 +26,12 @@ export default class Client {
|
||||||
headers: this.headers
|
headers: this.headers
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON stringify and append body to request if provided and is not a GET request
|
// JSON stringify and append body to request if provided
|
||||||
if (payload && method !== Method.GET) {
|
if (payload) {
|
||||||
this.setHeader("Content-Type", "application/json");
|
|
||||||
options["body"] = JSON.stringify(payload);
|
options["body"] = JSON.stringify(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch and return Response object
|
// Fetch and return Response object
|
||||||
return await fetch(this.getEndpoint(endpoint), options);
|
return await fetch(endpoint, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,7 +2,9 @@
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"emitDecoratorMetadata": true,
|
"emitDecoratorMetadata": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"module": "NodeNext",
|
"module": "amd",
|
||||||
"outDir": "./build"
|
"target": "ES6",
|
||||||
|
"rootDir": "./src",
|
||||||
|
"outFile": "./build/Reflect.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue