@xylabs/api
Advanced tools
Comparing version 2.10.18 to 2.11.0
@@ -1,36 +0,6 @@ | ||
declare enum ApiStage { | ||
Beta = "beta", | ||
Local = "local", | ||
Prod = "prod" | ||
} | ||
declare abstract class ApiClient { | ||
protected token?: string | null | undefined; | ||
protected stage?: ApiStage | undefined; | ||
constructor(token?: string | null | undefined, stage?: ApiStage | undefined); | ||
abstract endPoint(): string; | ||
} | ||
interface ApiConfig { | ||
apiDomain: string; | ||
apiKey?: string; | ||
jwtToken?: string; | ||
userid?: string; | ||
} | ||
declare class ApiEndpoint<T> { | ||
private _value?; | ||
private config; | ||
private path; | ||
constructor(config: ApiConfig, path: string); | ||
get value(): T | undefined; | ||
private get headers(); | ||
private get url(); | ||
fetch(): Promise<T>; | ||
get(): Promise<T | NonNullable<T>>; | ||
insert(value: T): Promise<T>; | ||
} | ||
declare const getApiStage: (hostname: string) => ApiStage; | ||
export { ApiClient, ApiConfig, ApiEndpoint, ApiStage, getApiStage }; | ||
export * from './ApiClient'; | ||
export * from './ApiConfig'; | ||
export * from './ApiEndpoint'; | ||
export * from './ApiStage'; | ||
export * from './getApiStage'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,117 +0,80 @@ | ||
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
'use strict'; | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
ApiClient: () => ApiClient, | ||
ApiEndpoint: () => ApiEndpoint, | ||
ApiStage: () => ApiStage, | ||
getApiStage: () => getApiStage | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var axios = require('axios'); | ||
// src/ApiStage.ts | ||
var ApiStage = /* @__PURE__ */ ((ApiStage2) => { | ||
ApiStage2["Beta"] = "beta"; | ||
ApiStage2["Local"] = "local"; | ||
ApiStage2["Prod"] = "prod"; | ||
return ApiStage2; | ||
})(ApiStage || {}); | ||
exports.ApiStage = void 0; | ||
(function (ApiStage) { | ||
ApiStage["Beta"] = "beta"; | ||
ApiStage["Local"] = "local"; | ||
ApiStage["Prod"] = "prod"; | ||
})(exports.ApiStage || (exports.ApiStage = {})); | ||
// src/ApiClient.ts | ||
var ApiClient = class { | ||
constructor(token, stage) { | ||
this.token = token; | ||
this.stage = stage; | ||
this.stage = stage ?? "prod" /* Prod */; | ||
this.token = token; | ||
} | ||
}; | ||
class ApiClient { | ||
token; | ||
stage; | ||
constructor(token, stage) { | ||
this.token = token; | ||
this.stage = stage; | ||
this.stage = stage ?? exports.ApiStage.Prod; | ||
this.token = token; | ||
} | ||
} | ||
// src/ApiEndpoint.ts | ||
var import_axios = __toESM(require("axios")); | ||
var ApiEndpoint = class { | ||
_value; | ||
config; | ||
path; | ||
constructor(config, path) { | ||
this.config = config; | ||
this.path = path; | ||
} | ||
get value() { | ||
return this._value; | ||
} | ||
get headers() { | ||
return this.config.jwtToken ? { Authorization: this.config.jwtToken } : void 0; | ||
} | ||
get url() { | ||
return `${this.config.apiDomain}/${this.path}`; | ||
} | ||
async fetch() { | ||
const response = await import_axios.default.get(this.url, { headers: this.headers }); | ||
if (response.status === 200) { | ||
this._value = response.data; | ||
} else { | ||
throw Error("Unexpected Status Code"); | ||
class ApiEndpoint { | ||
_value; | ||
config; | ||
path; | ||
constructor(config, path) { | ||
this.config = config; | ||
this.path = path; | ||
} | ||
return this._value; | ||
} | ||
async get() { | ||
return this._value ?? await this.fetch(); | ||
} | ||
async insert(value) { | ||
const response = await import_axios.default.post(this.url, value, { headers: this.headers }); | ||
if (response.status === 200) { | ||
this._value = response.data; | ||
} else { | ||
throw Error("Unexpected Status Code"); | ||
get value() { | ||
return this._value; | ||
} | ||
return this._value; | ||
} | ||
get headers() { | ||
return this.config.jwtToken ? { Authorization: this.config.jwtToken } : undefined; | ||
} | ||
get url() { | ||
return `${this.config.apiDomain}/${this.path}`; | ||
} | ||
async fetch() { | ||
const response = await axios.get(this.url, { headers: this.headers }); | ||
if (response.status === 200) { | ||
this._value = response.data; | ||
} | ||
else { | ||
throw Error('Unexpected Status Code'); | ||
} | ||
return this._value; | ||
} | ||
async get() { | ||
return this._value ?? (await this.fetch()); | ||
} | ||
async insert(value) { | ||
const response = await axios.post(this.url, value, { headers: this.headers }); | ||
if (response.status === 200) { | ||
this._value = response.data; | ||
} | ||
else { | ||
throw Error('Unexpected Status Code'); | ||
} | ||
return this._value; | ||
} | ||
} | ||
const getApiStage = (hostname) => { | ||
if (hostname.startsWith('localhost')) { | ||
return exports.ApiStage.Local; | ||
} | ||
else if (hostname.startsWith('beta.')) { | ||
return exports.ApiStage.Beta; | ||
} | ||
else { | ||
return exports.ApiStage.Prod; | ||
} | ||
}; | ||
// src/getApiStage.ts | ||
var getApiStage = (hostname) => { | ||
if (hostname.startsWith("localhost")) { | ||
return "local" /* Local */; | ||
} else if (hostname.startsWith("beta.")) { | ||
return "beta" /* Beta */; | ||
} else { | ||
return "prod" /* Prod */; | ||
} | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
ApiClient, | ||
ApiEndpoint, | ||
ApiStage, | ||
getApiStage | ||
}); | ||
//# sourceMappingURL=index.js.map | ||
exports.ApiClient = ApiClient; | ||
exports.ApiEndpoint = ApiEndpoint; | ||
exports.getApiStage = getApiStage; | ||
//# sourceMappingURL=index.js.map |
@@ -46,6 +46,2 @@ { | ||
"module": "dist/index.mjs", | ||
"scripts": { | ||
"package-compile": "tsup && publint", | ||
"package-recompile": "tsup && publint" | ||
}, | ||
"homepage": "https://xylabs.com", | ||
@@ -62,6 +58,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"@xylabs/ts-scripts-yarn3": "^2.19.12", | ||
"@xylabs/tsconfig": "^2.19.12", | ||
"publint": "^0.2.2", | ||
"tsup": "^7.2.0" | ||
"@xylabs/ts-scripts-yarn3": "^3.0.0-rc.15", | ||
"@xylabs/tsconfig": "^3.0.0-rc.15" | ||
}, | ||
@@ -76,4 +70,4 @@ "publishConfig": { | ||
"sideEffects": false, | ||
"version": "2.10.18", | ||
"version": "2.11.0", | ||
"packageManager": "yarn@3.3.1" | ||
} |
import { ApiStage } from './ApiStage' | ||
abstract class ApiClient { | ||
public constructor( | ||
constructor( | ||
protected token?: string | null, | ||
@@ -6,0 +6,0 @@ protected stage?: ApiStage, |
@@ -15,3 +15,3 @@ import axios from 'axios' | ||
public get value() { | ||
get value() { | ||
return this._value | ||
@@ -28,3 +28,3 @@ } | ||
public async fetch() { | ||
async fetch() { | ||
const response = await axios.get<T>(this.url, { headers: this.headers }) | ||
@@ -39,7 +39,7 @@ if (response.status === 200) { | ||
public async get() { | ||
async get() { | ||
return this._value ?? (await this.fetch()) | ||
} | ||
public async insert(value: T) { | ||
async insert(value: T) { | ||
const response = await axios.post<T>(this.url, value, { headers: this.headers }) | ||
@@ -46,0 +46,0 @@ if (response.status === 200) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
27
23174
271