mirror of
https://codeberg.org/reflect/reflect-client-js.git
synced 2025-09-14 07:33:40 +02:00
Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
5d6b4a4675 | |||
04e9c7ec84 | |||
87a5fd4cb9 | |||
278868984d |
4 changed files with 88 additions and 11 deletions
52
.npmignore
Normal file
52
.npmignore
Normal file
|
@ -0,0 +1,52 @@
|
|||
/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/
|
11
package.json
11
package.json
|
@ -1,7 +1,16 @@
|
|||
{
|
||||
"name": "reflect-client",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.2",
|
||||
"main": "build/Reflect.js",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": {
|
||||
"types": "./src/Reflect.ts",
|
||||
"default": "./build/Reflect.js"
|
||||
}
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.2.2"
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
import { Method } from "./Method";
|
||||
import { Method } from "./Method.js";
|
||||
|
||||
// Export Method as importable directly from the Reflect.js ESM
|
||||
export { Method };
|
||||
|
||||
// Connect to a Reflect API instance over HTTP
|
||||
export default class Client {
|
||||
|
@ -6,7 +9,8 @@ export default class Client {
|
|||
private headers: object = {};
|
||||
|
||||
constructor(url: string, key: string|null = null) {
|
||||
this.url = url;
|
||||
// Append tailing slash if omitted from URL string
|
||||
this.url = url.substring(url.length - 1) === "/" ? url : url + "/";
|
||||
|
||||
// Use API key with requests if defined
|
||||
if (key) {
|
||||
|
@ -14,9 +18,22 @@ 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
|
||||
private setApiKey(key: string): void {
|
||||
this.headers["Authentication"] = `Bearer ${key}`;
|
||||
return this.setHeader("Authorization", `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
|
||||
|
@ -26,12 +43,13 @@ export default class Client {
|
|||
headers: this.headers
|
||||
}
|
||||
|
||||
// JSON stringify and append body to request if provided
|
||||
if (payload) {
|
||||
// JSON stringify and append body to request if provided and is not a GET request
|
||||
if (payload && method !== Method.GET) {
|
||||
this.setHeader("Content-Type", "application/json");
|
||||
options["body"] = JSON.stringify(payload);
|
||||
}
|
||||
|
||||
// Fetch and return Response object
|
||||
return await fetch(endpoint, options);
|
||||
return await fetch(this.getEndpoint(endpoint), options);
|
||||
}
|
||||
}
|
|
@ -2,9 +2,7 @@
|
|||
"compilerOptions": {
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"module": "amd",
|
||||
"target": "ES6",
|
||||
"rootDir": "./src",
|
||||
"outFile": "./build/Reflect.js"
|
||||
"module": "NodeNext",
|
||||
"outDir": "./build"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue