mirror of
https://codeberg.org/reflect/reflect-client-js.git
synced 2025-09-13 23:43:41 +02:00
initial commit
This commit is contained in:
commit
fbea20b268
6 changed files with 152 additions and 0 deletions
52
.gitignore
vendored
Executable file
52
.gitignore
vendored
Executable file
|
@ -0,0 +1,52 @@
|
||||||
|
/build
|
||||||
|
|
||||||
|
# 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/
|
36
package-lock.json
generated
Normal file
36
package-lock.json
generated
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
{
|
||||||
|
"name": "reflect-client",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"lockfileVersion": 2,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"name": "reflect-client",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^5.2.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/typescript": {
|
||||||
|
"version": "5.2.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz",
|
||||||
|
"integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==",
|
||||||
|
"dev": true,
|
||||||
|
"bin": {
|
||||||
|
"tsc": "bin/tsc",
|
||||||
|
"tsserver": "bin/tsserver"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.17"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"typescript": {
|
||||||
|
"version": "5.2.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz",
|
||||||
|
"integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==",
|
||||||
|
"dev": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
package.json
Normal file
8
package.json
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"name": "reflect-client",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "build/Reflect.js",
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^5.2.2"
|
||||||
|
}
|
||||||
|
}
|
9
src/Method.ts
Normal file
9
src/Method.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
// Available HTTP request methods
|
||||||
|
export enum Method {
|
||||||
|
GET = "GET",
|
||||||
|
POST = "POST",
|
||||||
|
PUT = "PUT",
|
||||||
|
PATCH = "PATCH",
|
||||||
|
DELETE = "DELETE",
|
||||||
|
OPTIONS = "OPTIONS"
|
||||||
|
}
|
37
src/Reflect.ts
Normal file
37
src/Reflect.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import { Method } from "./Method";
|
||||||
|
|
||||||
|
// Connect to a Reflect API instance over HTTP
|
||||||
|
export default class Client {
|
||||||
|
private url: string;
|
||||||
|
private headers: object = {};
|
||||||
|
|
||||||
|
constructor(url: string, key: string|null = null) {
|
||||||
|
this.url = url;
|
||||||
|
|
||||||
|
// Use API key with requests if defined
|
||||||
|
if (key) {
|
||||||
|
this.setApiKey(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set API key to use for all requests
|
||||||
|
private setApiKey(key: string): void {
|
||||||
|
this.headers["Authentication"] = `Bearer ${key}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call a Reflect API endpoint with method and optional payload
|
||||||
|
public async call(endpoint: string, method: Method, payload: any = null): Promise<Response> {
|
||||||
|
const options: object = {
|
||||||
|
method: Method[method],
|
||||||
|
headers: this.headers
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSON stringify and append body to request if provided
|
||||||
|
if (payload) {
|
||||||
|
options["body"] = JSON.stringify(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch and return Response object
|
||||||
|
return await fetch(endpoint, options);
|
||||||
|
}
|
||||||
|
}
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"emitDecoratorMetadata": true,
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"module": "amd",
|
||||||
|
"target": "ES6",
|
||||||
|
"rootDir": "./src",
|
||||||
|
"outFile": "./build/Reflect.js"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue