tering-serieuze-sdk 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +4 -2
- package/dist/index.mjs.map +2 -2
- package/package.json +1 -1
- package/src/api/auth.ts +1 -1
- package/src/cache/cookieCache.ts +4 -4
- package/src/index.ts +9 -2
package/dist/index.mjs
CHANGED
|
@@ -246,10 +246,12 @@ var CacheType = /* @__PURE__ */ ((CacheType2) => {
|
|
|
246
246
|
return CacheType2;
|
|
247
247
|
})(CacheType || {});
|
|
248
248
|
var TssApi = class {
|
|
249
|
+
apiConfiguration;
|
|
249
250
|
auth;
|
|
250
251
|
jingle;
|
|
251
252
|
user;
|
|
252
|
-
constructor(cacheConfiguration) {
|
|
253
|
+
constructor(apiConfiguration, cacheConfiguration) {
|
|
254
|
+
this.apiConfiguration = apiConfiguration;
|
|
253
255
|
this.jingle = new JingleModule(this, cacheConfiguration?.jingle ?? new NullCache("jingle"));
|
|
254
256
|
this.auth = new AuthModule(this, cacheConfiguration?.auth ?? new NullCache("auth"));
|
|
255
257
|
this.user = new UserModule(this, cacheConfiguration?.user ?? new NullCache("user"));
|
|
@@ -261,7 +263,7 @@ var TssApi = class {
|
|
|
261
263
|
"Content-Type": "application/json",
|
|
262
264
|
Authorization: `Bearer ${await this.auth.getToken()}`
|
|
263
265
|
};
|
|
264
|
-
const apiPrefix =
|
|
266
|
+
const apiPrefix = this.apiConfiguration.API_URL;
|
|
265
267
|
const response = await fetch(apiPrefix + "/api/" + url, config);
|
|
266
268
|
switch (response.status) {
|
|
267
269
|
case 400:
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/cache/abstractCache.ts", "../src/cache/cookieCache.ts", "../src/cache/filesystemCache.ts", "../src/cache/nullCache.ts", "../src/cache/onePasswordCache.ts", "../src/api/base.ts", "../src/api/auth.ts", "../src/api/jingle.ts", "../src/api/user.ts", "../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { get } from \"stack-trace\";\n\nexport enum CacheLifetime {\n Day = 60 * 60 * 24,\n Week = 60 * 60 * 24 * 7,\n}\n\nexport abstract class AbstractCache {\n constructor(protected cacheName: string) {}\n\n public abstract cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T>;\n\n public abstract get<T>(key: string): Promise<T | undefined>;\n\n public abstract set<T>(key: string, value: T, lifetime?: number): void;\n\n public abstract remove(key: string): void;\n\n public abstract clear(): void;\n\n public getCacheKey(): string {\n const errorMessage = \"Could not determine cache key. Please provide a key manually.\";\n const trace = get();\n if (trace.length < 3) {\n throw new Error(errorMessage);\n }\n\n const functionName = get()[2].getFunctionName();\n if (!functionName) {\n throw new Error(errorMessage);\n }\n\n return functionName;\n }\n}\n", "import { AbstractCache } from \"./abstractCache\";\n\nexport class CookieCache extends AbstractCache {\n public cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T> {\n throw new Error(\"Not implemented, use get() and set()\");\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n if (document.cookie.includes(\"auth=\")) {\n return document.cookie.split(\"auth=\")[1]?.split(\";\")[0] as T;\n }\n\n return undefined;\n }\n\n public set<T>(key: string, value: T, lifetime?: number): void {\n const expires = lifetime ? new Date(Date.now() + lifetime).toUTCString() : \"\";\n document.cookie = `auth=${value}; expires=${expires}; path=/; samesite=strict; secure`;\n }\n\n public remove(key: string): void {\n document.cookie = \"auth=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;\";\n }\n\n public clear(): void {\n throw new Error(\"Not implemented, use remove()\");\n }\n}\n", "import { AbstractCache } from \"./abstractCache\";\nimport { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from \"fs\";\nimport path from \"path\";\nimport os from \"os\";\n\ntype CacheItem = {\n value: any;\n expiresAt?: number;\n};\n\nexport class FilesystemCache extends AbstractCache {\n protected initialized = false;\n\n protected state: { [key: string]: CacheItem } = {};\n\n public async cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T> {\n key = key ?? this.getCacheKey();\n\n const cachedValue = await this.get<T>(key);\n if (cachedValue) {\n return cachedValue;\n }\n\n const result = await callbackWhenEmpty();\n this.set(key, result, lifetime);\n return result;\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n if (!this.initialized) {\n await this._initialize();\n }\n\n const expires = this.state[key]?.expiresAt;\n if (Object.keys(this.state).includes(key) && (expires === undefined || expires > Date.now())) {\n return this.state[key].value;\n }\n\n return undefined;\n }\n\n public set<T>(key: string, value: T, lifetime?: number): void {\n const expiresAt = lifetime ? Date.now() + lifetime : undefined;\n const content: CacheItem = { value };\n if (expiresAt) {\n content.expiresAt = expiresAt;\n }\n this.state[key] = content;\n this.writeState();\n }\n\n public remove(key: string): void {\n delete this.state[key];\n this.writeState();\n }\n\n public clear(): void {\n this.state = {};\n unlinkSync(this.getFilePath());\n }\n\n public getFilePath() {\n return path.join(os.homedir(), \".tss\", `${this.cacheName}.json`);\n }\n\n protected _initialize() {\n this.initialized = true;\n if (existsSync(this.getFilePath())) {\n this.state = JSON.parse(readFileSync(this.getFilePath(), \"utf8\"));\n }\n }\n\n protected writeState() {\n if (!existsSync(path.dirname(this.getFilePath()))) {\n mkdirSync(path.dirname(this.getFilePath()));\n }\n\n writeFileSync(this.getFilePath(), JSON.stringify(this.state));\n }\n}\n", "import { AbstractCache } from \"./abstractCache\";\n\nexport class NullCache extends AbstractCache {\n public cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T> {\n return callbackWhenEmpty();\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n return undefined;\n }\n\n public set<T>(key: string, value: T, lifetime?: number): void {}\n\n public remove(key: string): void {}\n\n public clear(): void {}\n}\n", "import { AbstractCache } from \"./abstractCache\";\nimport { promisify } from \"util\";\nimport { exec } from \"child_process\";\n\n// TODO error handling en lifetime\nexport class OnePasswordCache extends AbstractCache {\n protected asyncExec = promisify(exec);\n\n public cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T> {\n throw new Error(\"Not implemented, use get() and set()\");\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n const { stdout, stderr } = await this.asyncExec(`op item get \"${key}\" --fields label=credential --format json`);\n\n if (stderr) {\n console.log(stderr);\n throw new Error(\"ERROR\");\n }\n\n return JSON.parse(stdout).value;\n }\n\n public async set<T>(key: string, value: T, lifetime?: number): Promise<void> {\n let action = `op item edit '${key}' 'credential=${value}'`;\n\n try {\n const t = await this.get(key);\n } catch (e) {\n action = `op item create --category=\"API Credential\" --title ${key} 'credential=${value}'`;\n }\n\n const { stdout, stderr } = await this.asyncExec(action);\n\n if (stderr) {\n console.log(stderr);\n throw new Error(\"ERROR\");\n }\n }\n\n public async remove(key: string): Promise<void> {\n await this.asyncExec(`op item delete '${key}'`);\n }\n\n public clear(): void {\n // Loopje maken en heel 1password leeg maken?\n }\n}\n", "import { AbstractCache } from \"../cache\";\nimport { TssApi } from \"../index\";\n\nexport class BaseModule {\n constructor(protected api: TssApi, protected cache: AbstractCache) {}\n\n public getCache() {\n return this.cache;\n }\n\n public setCache(cache: AbstractCache) {\n this.cache = cache;\n }\n}\n", "import { BaseModule } from \"./base\";\n\nexport class AuthModule extends BaseModule {\n protected static cacheKey = \"TSS-TOKEN\";\n\n async getToken(): Promise<string | undefined> {\n return this.cache.get(AuthModule.cacheKey);\n }\n\n async setToken(token: string) {\n return this.cache.set(AuthModule.cacheKey, token);\n }\n\n async removeToken() {\n return this.cache.remove(AuthModule.cacheKey);\n }\n}\n", "import { JingleFolder, Jingle } from \"tering-serieuze-types\";\nimport { BaseModule } from \"./base\";\nimport { CacheLifetime } from \"../cache\";\n\nexport class JingleModule extends BaseModule {\n async getGrouped() {\n return this.cache.cacheOrGet(\n () => {\n return this.api.fetch<JingleFolder[]>(\"jingle/grouped\");\n },\n \"getGrouped\",\n CacheLifetime.Week\n );\n }\n\n async getAll() {\n return this.cache.cacheOrGet(\n () => {\n return this.api.fetch<Jingle[]>(\"jingle\");\n },\n \"getAll\",\n CacheLifetime.Week\n );\n }\n\n async play(folder: string, file: string) {\n console.log(`Playing jingle ${folder}/${file}`);\n return this.api.post(\"jingle/play\", { folder, file });\n }\n}\n", "import { BaseModule } from \"./base\";\nimport { User } from \"tering-serieuze-types\";\n\nexport class UserModule extends BaseModule {\n async get(userId?: string) {\n return this.cache.cacheOrGet(() => {\n const userIdParam = userId ? `/${userId}` : \"\";\n return this.api.fetch<User>(`user${userIdParam}`);\n });\n }\n}\n", "import { AbstractCache, NullCache } from \"./cache\";\nimport { AuthModule, JingleModule, UserModule } from \"./api\";\nimport {\n BadRequestException,\n ForbiddenException,\n NotFoundException,\n UnauthorizedException,\n UnprocessableEntityException,\n} from \"@nestjs/common\";\n\nexport * from \"./api\";\nexport * from \"./cache\";\n\nexport interface SDKRequestInit extends RequestInit {\n method?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\" | \"PATCH\";\n}\n\nexport enum CacheType {\n jingle = \"jingle\",\n auth = \"auth\",\n user = \"user\",\n}\n\nexport type CacheConfiguration = {\n [key in keyof typeof CacheType]?: AbstractCache;\n};\n\nexport class TssApi {\n auth: AuthModule;\n\n jingle: JingleModule;\n\n user: UserModule;\n\n constructor(cacheConfiguration?: CacheConfiguration) {\n this.jingle = new JingleModule(this, cacheConfiguration?.jingle ?? new NullCache(\"jingle\"));\n this.auth = new AuthModule(this, cacheConfiguration?.auth ?? new NullCache(\"auth\"));\n this.user = new UserModule(this, cacheConfiguration?.user ?? new NullCache(\"user\"));\n }\n\n async fetch<T>(url: string, config: SDKRequestInit = {}) {\n config.method = config.method ?? \"GET\";\n config.headers = {\n ...config.headers,\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${await this.auth.getToken()}`,\n };\n\n const apiPrefix = process.env.API_URL ?? \"https://tss.maxserv.dev:8081\";\n const response = await fetch(apiPrefix + \"/api/\" + url, config);\n switch (response.status) {\n case 400:\n throw new BadRequestException(response.statusText);\n case 401:\n throw new UnauthorizedException(response.statusText);\n case 403:\n throw new ForbiddenException(response.statusText);\n case 404:\n throw new NotFoundException(response.statusText);\n case 422:\n throw new UnprocessableEntityException(response.statusText);\n }\n\n return response.json() as Promise<T>;\n }\n\n async post<T>(url: string, body: any) {\n return this.fetch<T>(url, {\n method: \"POST\",\n body: JSON.stringify(body),\n });\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;AAAA,SAAS,WAAW;AAEb,IAAK,gBAAL,kBAAKA,mBAAL;AACH,EAAAA,8BAAA,SAAM,SAAN;AACA,EAAAA,8BAAA,UAAO,UAAP;AAFQ,SAAAA;AAAA,GAAA;AAKL,IAAe,gBAAf,MAA6B;AAAA,EAChC,YAAsB,WAAmB;AAAnB;AAAA,EAAoB;AAAA,EAYnC,cAAsB;AACzB,UAAM,eAAe;AACrB,UAAM,QAAQ,IAAI;AAClB,QAAI,MAAM,SAAS,GAAG;AAClB,YAAM,IAAI,MAAM,YAAY;AAAA,IAChC;AAEA,UAAM,eAAe,IAAI,EAAE,CAAC,EAAE,gBAAgB;AAC9C,QAAI,CAAC,cAAc;AACf,YAAM,IAAI,MAAM,YAAY;AAAA,IAChC;AAEA,WAAO;AAAA,EACX;AACJ;;;AChCO,IAAM,cAAN,cAA0B,cAAc;AAAA,EACpC,WAAc,mBAAqC,KAAc,UAA+B;AACnG,UAAM,IAAI,MAAM,sCAAsC;AAAA,EAC1D;AAAA,EAEA,MAAa,IAAO,KAAqC;AACrD,QAAI,SAAS,OAAO,SAAS,OAAO,GAAG;AACnC,aAAO,SAAS,OAAO,MAAM,OAAO,EAAE,CAAC,GAAG,MAAM,GAAG,EAAE,CAAC;AAAA,IAC1D;AAEA,WAAO;AAAA,EACX;AAAA,EAEO,IAAO,KAAa,OAAU,UAAyB;AAC1D,UAAM,UA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AAO,KAAK,IAAI,MAAsB,gBAAgB;AAAA,MAC1D;AAAA,MACA;AAAA;AAAA,IAEJ;AAAA,EACJ;AAAA,EAEA,MAAM,SAAS;AACX,WAAO,KAAK,MAAM;AAAA,MACd,MAAM;AACF,eAAO,KAAK,IAAI,MAAgB,QAAQ;AAAA,MAC5C;AAAA,MACA;AAAA;AAAA,IAEJ;AAAA,EACJ;AAAA,EAEA,MAAM,KAAK,QAAgB,MAAc;AACrC,YAAQ,IAAI,kBAAkB,UAAU,MAAM;AAC9C,WAAO,KAAK,IAAI,KAAK,eAAe,EAAE,QAAQ,KAAK,CAAC;AAAA,EACxD;AACJ;;;AC1BO,IAAM,aAAN,cAAyB,WAAW;AAAA,EACvC,MAAM,IAAI,QAAiB;AACvB,WAAO,KAAK,MAAM,WAAW,MAAM;AAC/B,YAAM,cAAc,SAAS,IAAI,WAAW;AAC5C,aAAO,KAAK,IAAI,MAAY,OAAO,aAAa;AAAA,IACpD,CAAC;AAAA,EACL;AACJ;;;ACRA;AAAA,EACI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AASA,IAAK,YAAL,kBAAKC,eAAL;AACH,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,UAAO;AACP,EAAAA,WAAA,UAAO;AAHC,SAAAA;AAAA,GAAA;
|
|
4
|
+
"sourcesContent": ["import { get } from \"stack-trace\";\n\nexport enum CacheLifetime {\n Day = 60 * 60 * 24,\n Week = 60 * 60 * 24 * 7,\n}\n\nexport abstract class AbstractCache {\n constructor(protected cacheName: string) {}\n\n public abstract cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T>;\n\n public abstract get<T>(key: string): Promise<T | undefined>;\n\n public abstract set<T>(key: string, value: T, lifetime?: number): void;\n\n public abstract remove(key: string): void;\n\n public abstract clear(): void;\n\n public getCacheKey(): string {\n const errorMessage = \"Could not determine cache key. Please provide a key manually.\";\n const trace = get();\n if (trace.length < 3) {\n throw new Error(errorMessage);\n }\n\n const functionName = get()[2].getFunctionName();\n if (!functionName) {\n throw new Error(errorMessage);\n }\n\n return functionName;\n }\n}\n", "import { AbstractCache } from \"./abstractCache\";\n\nexport class CookieCache extends AbstractCache {\n public cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T> {\n throw new Error(\"Not implemented, use get() and set()\");\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n if (document.cookie.includes(\"auth=\")) {\n return document.cookie.split(\"auth=\")[1]?.split(\";\")[0] as T;\n }\n\n return undefined;\n }\n\n public set<T>(key: string, value: T, lifetime?: number): void {\n const expires = lifetime ? new Date(Date.now() + lifetime).toUTCString() : \"\";\n document.cookie = `auth=${value}; expires=${expires}; path=/; samesite=strict; secure`;\n }\n\n public remove(key: string): void {\n document.cookie = \"auth=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;\";\n }\n\n public clear(): void {\n throw new Error(\"Not implemented, use remove()\");\n }\n}\n", "import { AbstractCache } from \"./abstractCache\";\nimport { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from \"fs\";\nimport path from \"path\";\nimport os from \"os\";\n\ntype CacheItem = {\n value: any;\n expiresAt?: number;\n};\n\nexport class FilesystemCache extends AbstractCache {\n protected initialized = false;\n\n protected state: { [key: string]: CacheItem } = {};\n\n public async cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T> {\n key = key ?? this.getCacheKey();\n\n const cachedValue = await this.get<T>(key);\n if (cachedValue) {\n return cachedValue;\n }\n\n const result = await callbackWhenEmpty();\n this.set(key, result, lifetime);\n return result;\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n if (!this.initialized) {\n await this._initialize();\n }\n\n const expires = this.state[key]?.expiresAt;\n if (Object.keys(this.state).includes(key) && (expires === undefined || expires > Date.now())) {\n return this.state[key].value;\n }\n\n return undefined;\n }\n\n public set<T>(key: string, value: T, lifetime?: number): void {\n const expiresAt = lifetime ? Date.now() + lifetime : undefined;\n const content: CacheItem = { value };\n if (expiresAt) {\n content.expiresAt = expiresAt;\n }\n this.state[key] = content;\n this.writeState();\n }\n\n public remove(key: string): void {\n delete this.state[key];\n this.writeState();\n }\n\n public clear(): void {\n this.state = {};\n unlinkSync(this.getFilePath());\n }\n\n public getFilePath() {\n return path.join(os.homedir(), \".tss\", `${this.cacheName}.json`);\n }\n\n protected _initialize() {\n this.initialized = true;\n if (existsSync(this.getFilePath())) {\n this.state = JSON.parse(readFileSync(this.getFilePath(), \"utf8\"));\n }\n }\n\n protected writeState() {\n if (!existsSync(path.dirname(this.getFilePath()))) {\n mkdirSync(path.dirname(this.getFilePath()));\n }\n\n writeFileSync(this.getFilePath(), JSON.stringify(this.state));\n }\n}\n", "import { AbstractCache } from \"./abstractCache\";\n\nexport class NullCache extends AbstractCache {\n public cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T> {\n return callbackWhenEmpty();\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n return undefined;\n }\n\n public set<T>(key: string, value: T, lifetime?: number): void {}\n\n public remove(key: string): void {}\n\n public clear(): void {}\n}\n", "import { AbstractCache } from \"./abstractCache\";\nimport { promisify } from \"util\";\nimport { exec } from \"child_process\";\n\n// TODO error handling en lifetime\nexport class OnePasswordCache extends AbstractCache {\n protected asyncExec = promisify(exec);\n\n public cacheOrGet<T>(callbackWhenEmpty: () => Promise<T>, key?: string, lifetime?: number): Promise<T> {\n throw new Error(\"Not implemented, use get() and set()\");\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n const { stdout, stderr } = await this.asyncExec(`op item get \"${key}\" --fields label=credential --format json`);\n\n if (stderr) {\n console.log(stderr);\n throw new Error(\"ERROR\");\n }\n\n return JSON.parse(stdout).value;\n }\n\n public async set<T>(key: string, value: T, lifetime?: number): Promise<void> {\n let action = `op item edit '${key}' 'credential=${value}'`;\n\n try {\n const t = await this.get(key);\n } catch (e) {\n action = `op item create --category=\"API Credential\" --title ${key} 'credential=${value}'`;\n }\n\n const { stdout, stderr } = await this.asyncExec(action);\n\n if (stderr) {\n console.log(stderr);\n throw new Error(\"ERROR\");\n }\n }\n\n public async remove(key: string): Promise<void> {\n await this.asyncExec(`op item delete '${key}'`);\n }\n\n public clear(): void {\n // Loopje maken en heel 1password leeg maken?\n }\n}\n", "import { AbstractCache } from \"../cache\";\nimport { TssApi } from \"../index\";\n\nexport class BaseModule {\n constructor(protected api: TssApi, protected cache: AbstractCache) {}\n\n public getCache() {\n return this.cache;\n }\n\n public setCache(cache: AbstractCache) {\n this.cache = cache;\n }\n}\n", "import { BaseModule } from \"./base\";\n\nexport class AuthModule extends BaseModule {\n protected static cacheKey = \"TSS-TOKEN\";\n\n async getToken(): Promise<string | undefined> {\n return this.cache.get(AuthModule.cacheKey);\n }\n\n async setToken(token: string) {\n return this.cache.set(AuthModule.cacheKey, token);\n }\n\n async removeToken() {\n return this.cache.remove(AuthModule.cacheKey);\n }\n}\n", "import { JingleFolder, Jingle } from \"tering-serieuze-types\";\nimport { BaseModule } from \"./base\";\nimport { CacheLifetime } from \"../cache\";\n\nexport class JingleModule extends BaseModule {\n async getGrouped() {\n return this.cache.cacheOrGet(\n () => {\n return this.api.fetch<JingleFolder[]>(\"jingle/grouped\");\n },\n \"getGrouped\",\n CacheLifetime.Week\n );\n }\n\n async getAll() {\n return this.cache.cacheOrGet(\n () => {\n return this.api.fetch<Jingle[]>(\"jingle\");\n },\n \"getAll\",\n CacheLifetime.Week\n );\n }\n\n async play(folder: string, file: string) {\n console.log(`Playing jingle ${folder}/${file}`);\n return this.api.post(\"jingle/play\", { folder, file });\n }\n}\n", "import { BaseModule } from \"./base\";\nimport { User } from \"tering-serieuze-types\";\n\nexport class UserModule extends BaseModule {\n async get(userId?: string) {\n return this.cache.cacheOrGet(() => {\n const userIdParam = userId ? `/${userId}` : \"\";\n return this.api.fetch<User>(`user${userIdParam}`);\n });\n }\n}\n", "import { AbstractCache, NullCache } from \"./cache\";\nimport { AuthModule, JingleModule, UserModule } from \"./api\";\nimport {\n BadRequestException,\n ForbiddenException,\n NotFoundException,\n UnauthorizedException,\n UnprocessableEntityException,\n} from \"@nestjs/common\";\n\nexport * from \"./api\";\nexport * from \"./cache\";\n\nexport interface SDKRequestInit extends RequestInit {\n method?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\" | \"PATCH\";\n}\n\nexport enum CacheType {\n jingle = \"jingle\",\n auth = \"auth\",\n user = \"user\",\n}\n\nexport type ApiConfiguration = {\n API_URL: string;\n};\n\nexport type CacheConfiguration = {\n [key in keyof typeof CacheType]?: AbstractCache;\n};\n\nexport class TssApi {\n apiConfiguration: ApiConfiguration;\n\n auth: AuthModule;\n\n jingle: JingleModule;\n\n user: UserModule;\n\n constructor(apiConfiguration: ApiConfiguration, cacheConfiguration?: CacheConfiguration) {\n this.apiConfiguration = apiConfiguration;\n this.jingle = new JingleModule(this, cacheConfiguration?.jingle ?? new NullCache(\"jingle\"));\n this.auth = new AuthModule(this, cacheConfiguration?.auth ?? new NullCache(\"auth\"));\n this.user = new UserModule(this, cacheConfiguration?.user ?? new NullCache(\"user\"));\n }\n\n async fetch<T>(url: string, config: SDKRequestInit = {}) {\n config.method = config.method ?? \"GET\";\n config.headers = {\n ...config.headers,\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${await this.auth.getToken()}`,\n };\n\n const apiPrefix = this.apiConfiguration.API_URL;\n const response = await fetch(apiPrefix + \"/api/\" + url, config);\n switch (response.status) {\n case 400:\n throw new BadRequestException(response.statusText);\n case 401:\n throw new UnauthorizedException(response.statusText);\n case 403:\n throw new ForbiddenException(response.statusText);\n case 404:\n throw new NotFoundException(response.statusText);\n case 422:\n throw new UnprocessableEntityException(response.statusText);\n }\n\n return response.json() as Promise<T>;\n }\n\n async post<T>(url: string, body: any) {\n return this.fetch<T>(url, {\n method: \"POST\",\n body: JSON.stringify(body),\n });\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;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AS,UAAU;AAAA,MACvD,KAAK;AACD,cAAM,IAAI,mBAAmB,SAAS,UAAU;AAAA,MACpD,KAAK;AACD,cAAM,IAAI,kBAAkB,SAAS,UAAU;AAAA,MACnD,KAAK;AACD,cAAM,IAAI,6BAA6B,SAAS,UAAU;AAAA,IAClE;AAEA,WAAO,SAAS,KAAK;AAAA,EACzB;AAAA,EAEA,MAAM,KAAQ,KAAa,MAAW;AAClC,WAAO,KAAK,MAAS,KAAK;AAAA,MACtB,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,IAAI;AAAA,IAC7B,CAAC;AAAA,EACL;AACJ;",
|
|
6
6
|
"names": ["CacheLifetime", "CacheType"]
|
|
7
7
|
}
|
package/package.json
CHANGED
package/src/api/auth.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseModule } from "./base";
|
|
2
2
|
|
|
3
3
|
export class AuthModule extends BaseModule {
|
|
4
|
-
|
|
4
|
+
public static cacheKey = "TSS-TOKEN";
|
|
5
5
|
|
|
6
6
|
async getToken(): Promise<string | undefined> {
|
|
7
7
|
return this.cache.get(AuthModule.cacheKey);
|
package/src/cache/cookieCache.ts
CHANGED
|
@@ -6,8 +6,8 @@ export class CookieCache extends AbstractCache {
|
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
public async get<T>(key: string): Promise<T | undefined> {
|
|
9
|
-
if (document.cookie.includes(
|
|
10
|
-
return document.cookie.split(
|
|
9
|
+
if (document.cookie.includes(`${key}=`)) {
|
|
10
|
+
return document.cookie.split(`${key}=`)[1]?.split(";")[0] as T;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
return undefined;
|
|
@@ -15,11 +15,11 @@ export class CookieCache extends AbstractCache {
|
|
|
15
15
|
|
|
16
16
|
public set<T>(key: string, value: T, lifetime?: number): void {
|
|
17
17
|
const expires = lifetime ? new Date(Date.now() + lifetime).toUTCString() : "";
|
|
18
|
-
document.cookie =
|
|
18
|
+
document.cookie = `${key}=${value}; expires=${expires}; path=/; samesite=strict; secure`;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
public remove(key: string): void {
|
|
22
|
-
document.cookie =
|
|
22
|
+
document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
public clear(): void {
|
package/src/index.ts
CHANGED
|
@@ -21,18 +21,25 @@ export enum CacheType {
|
|
|
21
21
|
user = "user",
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
export type ApiConfiguration = {
|
|
25
|
+
API_URL: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
24
28
|
export type CacheConfiguration = {
|
|
25
29
|
[key in keyof typeof CacheType]?: AbstractCache;
|
|
26
30
|
};
|
|
27
31
|
|
|
28
32
|
export class TssApi {
|
|
33
|
+
apiConfiguration: ApiConfiguration;
|
|
34
|
+
|
|
29
35
|
auth: AuthModule;
|
|
30
36
|
|
|
31
37
|
jingle: JingleModule;
|
|
32
38
|
|
|
33
39
|
user: UserModule;
|
|
34
40
|
|
|
35
|
-
constructor(cacheConfiguration?: CacheConfiguration) {
|
|
41
|
+
constructor(apiConfiguration: ApiConfiguration, cacheConfiguration?: CacheConfiguration) {
|
|
42
|
+
this.apiConfiguration = apiConfiguration;
|
|
36
43
|
this.jingle = new JingleModule(this, cacheConfiguration?.jingle ?? new NullCache("jingle"));
|
|
37
44
|
this.auth = new AuthModule(this, cacheConfiguration?.auth ?? new NullCache("auth"));
|
|
38
45
|
this.user = new UserModule(this, cacheConfiguration?.user ?? new NullCache("user"));
|
|
@@ -46,7 +53,7 @@ export class TssApi {
|
|
|
46
53
|
Authorization: `Bearer ${await this.auth.getToken()}`,
|
|
47
54
|
};
|
|
48
55
|
|
|
49
|
-
const apiPrefix =
|
|
56
|
+
const apiPrefix = this.apiConfiguration.API_URL;
|
|
50
57
|
const response = await fetch(apiPrefix + "/api/" + url, config);
|
|
51
58
|
switch (response.status) {
|
|
52
59
|
case 400:
|