docs(changeset): feat(error-manager): implement centralized error reporting system

This commit is contained in:
2026-02-07 21:11:48 +02:00
parent 7bf94f306a
commit d7b1eb26f3
18 changed files with 806 additions and 187 deletions

View File

@@ -1,35 +1,34 @@
import {b64EncodeUnicode} from '@warkypublic/artemis-kit/base64'
const TOKEN_KEY = 'gridler_golang_restapi_v2_token'
/* eslint-disable @typescript-eslint/no-explicit-any */
import { b64EncodeUnicode } from '@warkypublic/artemis-kit/base64';
const TOKEN_KEY = 'gridler_golang_restapi_v2_token';
export type APIOptionsType = {
autocreate?: boolean
autoref?: boolean
baseurl?: string
getAPIProvider?: () => { provider: string; providerKey: string }
getAuthToken?: () => string
operations?: Array<FetchAPIOperation>
postfix?: string
prefix?: string
requestTimeoutSec?: number
}
autocreate?: boolean;
autoref?: boolean;
baseurl?: string;
getAPIProvider?: () => { provider: string; providerKey: string };
getAuthToken?: () => string;
operations?: Array<FetchAPIOperation>;
postfix?: string;
prefix?: string;
requestTimeoutSec?: number;
};
export interface APIResponse {
errmsg: string
payload?: any
retval: number
errmsg: string;
payload?: any;
retval: number;
}
export interface FetchAPIOperation {
name?: string
op?: string
type: GoAPIHeaderTypes //x-fieldfilter
value: string
name?: string;
op?: string;
type: GoAPIHeaderTypes; //x-fieldfilter
value: string;
}
/**
* @description Types for the Go Rest API headers
* @typedef {String} GoAPIEnum
*/
*/
export type GoAPIEnum =
| 'advsql'
| 'api-key'
@@ -42,7 +41,7 @@ export type GoAPIEnum =
| 'association_autoupdate'
| 'association-update'
| 'cql-sel'
| 'cursor-backward'// For x cursor-backward header
| 'cursor-backward' // For x cursor-backward header
| 'cursor-forward' // For x cursor-forward header
| 'custom-sql-join'
| 'custom-sql-or'
@@ -72,28 +71,24 @@ export type GoAPIEnum =
| 'simpleapi'
| 'skipcache'
| 'skipcount'
| 'sort'
| 'sort';
export type GoAPIHeaderKeys = `x-${GoAPIEnum}`;
export type GoAPIHeaderKeys = `x-${GoAPIEnum}`
export type GoAPIHeaderTypes = GoAPIEnum & string
export type GoAPIHeaderTypes = GoAPIEnum & string;
export interface GoAPIOperation {
name?: string
op?: string
type: GoAPIHeaderTypes //x-fieldfilter
value: string
name?: string;
op?: string;
type: GoAPIHeaderTypes; //x-fieldfilter
value: string;
}
export interface MetaData {
limit?: number
offset?: number
total?: number
limit?: number;
offset?: number;
total?: number;
}
/**
* Builds an array of objects by encoding specific values and setting headers.
*
@@ -105,50 +100,49 @@ const buildGoAPIOperation = (
ops: Array<FetchAPIOperation>,
headers?: Headers
): Array<FetchAPIOperation> => {
const newops = [...ops.filter((i) => i !== undefined && i.type !== undefined)]
const newops = [...ops.filter((i) => i !== undefined && i.type !== undefined)];
for (let i = 0; i < newops.length; i++) {
if (!newops[i].name || newops[i].name === '') {
newops[i].name = ''
newops[i].name = '';
}
if (newops[i].type === 'files' && !newops[i].value.startsWith('__')) {
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`;
}
if (newops[i].type === 'advsql' && !newops[i].value.startsWith('__')) {
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`;
}
if (newops[i].type === 'custom-sql-or' && !newops[i].value.startsWith('__')) {
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`;
}
if (newops[i].type === 'custom-sql-join' && !newops[i].value.startsWith('__')) {
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`;
}
if (newops[i].type === 'not-select-fields' && !newops[i].value.startsWith('__')) {
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`;
}
if (newops[i].type === 'custom-sql-w' && !newops[i].value.startsWith('__')) {
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`;
}
if (newops[i].type === 'select-fields' && !newops[i].value.startsWith('__')) {
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`;
}
if (newops[i].type === 'cql-sel' && !newops[i].value.startsWith('__')) {
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`
newops[i].value = `__${b64EncodeUnicode(newops[i].value)}__`;
}
if (headers) {
if (!newops || newops.length === 0) {
headers.set(`x-limit`, '10')
headers.set(`x-limit`, '10');
}
if (newops[i].type === 'association_autoupdate') {
headers.set(`association_autoupdate`, newops[i].value ?? '1')
headers.set(`association_autoupdate`, newops[i].value ?? '1');
}
if (newops[i].type === 'association_autocreate') {
headers.set(`association_autocreate`, newops[i].value ?? '1')
headers.set(`association_autocreate`, newops[i].value ?? '1');
}
if (
newops[i].type === 'searchop' ||
@@ -158,20 +152,20 @@ const buildGoAPIOperation = (
headers.set(
encodeURIComponent(`x-${newops[i].type}-${newops[i].op}-${newops[i].name}`),
String(newops[i].value)
)
);
} else {
headers.set(
encodeURIComponent(
`x-${newops[i].type}${newops[i].name && newops[i].name !== '' ? '-' + newops[i].name : ''}`
),
String(newops[i].value)
)
);
}
}
}
return newops
}
return newops;
};
/**
* Retrieves the headers from an array of FetchAPIOperation objects and returns them as an object.
@@ -183,77 +177,75 @@ const GoAPIHeaders = (
ops: Array<FetchAPIOperation>,
headers?: Headers
): { [key: string]: string } => {
const head = new Headers()
const headerlist: Record<string,string> = {}
const head = new Headers();
const headerlist: Record<string, string> = {};
const authToken = getAuthToken?.()
const authToken = getAuthToken?.();
if (authToken && authToken !== '') {
head.set('Authorization', `Token ${authToken}`)
head.set('Authorization', `Token ${authToken}`);
} else {
const token = getAuthToken()
const token = getAuthToken();
if (token) {
head.set('Authorization', `Token ${token}`)
head.set('Authorization', `Token ${token}`);
}
}
if (headers) {
headers.forEach((v, k) => {
head.set(k, v)
})
head.set(k, v);
});
}
const distinctOperations: Array<FetchAPIOperation> = []
const distinctOperations: Array<FetchAPIOperation> = [];
for (const value of ops?.filter((val) => !!val) ?? []) {
const index = distinctOperations.findIndex(
(searchValue) => searchValue.name === value.name && searchValue.type === value.type
)
);
if (index === -1) {
distinctOperations.push(value)
distinctOperations.push(value);
} else {
distinctOperations[index] = value
distinctOperations[index] = value;
}
}
buildGoAPIOperation(distinctOperations, head)
buildGoAPIOperation(distinctOperations, head);
head?.forEach((v, k) => {
headerlist[k] = v
})
headerlist[k] = v;
});
if (headers) {
for (const key of Object.keys(headerlist)) {
headers.set(key, headerlist[key])
headers.set(key, headerlist[key]);
}
}
return headerlist
}
return headerlist;
};
const callbacks = {
getAuthToken: () => {
getAuthToken: () => {
if (localStorage) {
const token = localStorage.getItem(TOKEN_KEY)
if (token) {
return token
}
const token = localStorage.getItem(TOKEN_KEY);
if (token) {
return token;
}
}
return undefined
}
}
return undefined;
},
};
/**
* Retrieves the authentication token from local storage.
*
* @return {string | undefined} The authentication token if found, otherwise undefined
*/
const getAuthToken = () => callbacks?.getAuthToken?.()
const getAuthToken = () => callbacks?.getAuthToken?.();
const setAuthTokenCallback = (cb: ()=> string) => {
callbacks.getAuthToken = cb
return callbacks.getAuthToken
}
const setAuthTokenCallback = (cb: () => string) => {
callbacks.getAuthToken = cb;
return callbacks.getAuthToken;
};
/**
* Sets the authentication token in the local storage.
@@ -262,9 +254,8 @@ const setAuthTokenCallback = (cb: ()=> string) => {
*/
const setAuthToken = (token: string) => {
if (localStorage) {
localStorage.setItem(TOKEN_KEY, token)
localStorage.setItem(TOKEN_KEY, token);
}
}
};
export {buildGoAPIOperation,getAuthToken,GoAPIHeaders,setAuthToken,setAuthTokenCallback}
export { buildGoAPIOperation, getAuthToken, GoAPIHeaders, setAuthToken, setAuthTokenCallback };