From fbea20b268bddf095250cbdaa9163b56a748ef94 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Wed, 20 Sep 2023 14:05:42 +0200 Subject: [PATCH] initial commit --- .gitignore | 52 +++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 36 ++++++++++++++++++++++++++++++++ package.json | 8 ++++++++ src/Method.ts | 9 ++++++++ src/Reflect.ts | 37 +++++++++++++++++++++++++++++++++ tsconfig.json | 10 +++++++++ 6 files changed, 152 insertions(+) create mode 100755 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/Method.ts create mode 100644 src/Reflect.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..c656acf --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..6db1050 --- /dev/null +++ b/package-lock.json @@ -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 + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..64db716 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "name": "reflect-client", + "version": "1.0.0", + "main": "build/Reflect.js", + "devDependencies": { + "typescript": "^5.2.2" + } +} diff --git a/src/Method.ts b/src/Method.ts new file mode 100644 index 0000000..a005b03 --- /dev/null +++ b/src/Method.ts @@ -0,0 +1,9 @@ +// Available HTTP request methods +export enum Method { + GET = "GET", + POST = "POST", + PUT = "PUT", + PATCH = "PATCH", + DELETE = "DELETE", + OPTIONS = "OPTIONS" +} \ No newline at end of file diff --git a/src/Reflect.ts b/src/Reflect.ts new file mode 100644 index 0000000..1b6a865 --- /dev/null +++ b/src/Reflect.ts @@ -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 { + 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); + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f74e32a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "module": "amd", + "target": "ES6", + "rootDir": "./src", + "outFile": "./build/Reflect.js" + } +} \ No newline at end of file