@chatium/sdk
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,3 +0,4 @@ | ||
import { AccountCtx, AppCtx, AuthCtx } from '../context'; | ||
import { AccountCtx, AppCtx } from '../context'; | ||
import { AuthCtx, OptionalAuthCtx } from '../ChatiumAuth'; | ||
export declare function chatiumGet<R = unknown>(ctx: AccountCtx & AppCtx & AuthCtx, url: string): Promise<R>; | ||
export declare function chatiumPost<R = unknown, P = unknown>(ctx: AccountCtx & AppCtx & AuthCtx, url: string, params?: P): Promise<R>; | ||
export declare function chatiumPost<R = unknown, P = unknown>(ctx: AccountCtx & AppCtx & OptionalAuthCtx, url: string, params?: P): Promise<R>; |
@@ -39,3 +39,9 @@ "use strict"; | ||
function createChatiumApiToken(ctx, method, url) { | ||
return jsonwebtoken_1.sign({ authToken: ctx.auth.requestToken, iat: Math.ceil(Date.now() / 1000) }, ctx.app.apiSecret + method + url); | ||
const token = { | ||
iat: Math.ceil(Date.now() / 1000) | ||
}; | ||
if (ctx.auth) { | ||
token.tkn = ctx.auth.requestToken; | ||
} | ||
return jsonwebtoken_1.sign(token, ctx.app.apiSecret + method + url); | ||
} |
@@ -0,1 +1,3 @@ | ||
import { OptionalUserCtx } from './ChatiumUser'; | ||
import { OptionalAuthCtx } from './ChatiumAuth'; | ||
export interface AppCtx { | ||
@@ -13,15 +15,5 @@ app: { | ||
} | ||
export interface AuthCtx { | ||
auth: { | ||
id: number; | ||
type: AuthType; | ||
key: string; | ||
requestToken: string; | ||
}; | ||
export declare function getChatiumContext(ctx: AppCtx, headers: ChatiumHeaders): AppCtx & AccountCtx & OptionalAuthCtx & OptionalUserCtx; | ||
export interface ChatiumHeaders { | ||
'x-chatium-application'?: string; | ||
} | ||
export declare enum AuthType { | ||
Email = "Email", | ||
None = "None", | ||
Phone = "Phone", | ||
Session = "Session" | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuthType = void 0; | ||
var AuthType; | ||
(function (AuthType) { | ||
AuthType["Email"] = "Email"; | ||
AuthType["None"] = "None"; | ||
AuthType["Phone"] = "Phone"; | ||
AuthType["Session"] = "Session"; | ||
})(AuthType = exports.AuthType || (exports.AuthType = {})); | ||
exports.getChatiumContext = void 0; | ||
const ChatiumAppRequestToken_1 = require("./ChatiumAppRequestToken"); | ||
const errors_1 = require("./errors"); | ||
function getChatiumContext(ctx, headers) { | ||
if (typeof headers['x-chatium-application'] !== 'string') { | ||
throw new errors_1.ApiError('x-chatium-application header is required!'); | ||
} | ||
const token = ChatiumAppRequestToken_1.validateChatiumAppRequestToken(headers['x-chatium-application'], ctx.app.apiSecret); | ||
return { | ||
...ctx, | ||
account: { | ||
id: token.acc, | ||
host: token.host, | ||
}, | ||
auth: token.aid | ||
? { | ||
id: token.aid, | ||
type: token.atp, | ||
requestToken: token.tkn, | ||
} | ||
: null, | ||
user: token.uid | ||
? { | ||
id: token.uid, | ||
roles: token.urs, | ||
firstName: token.ufn || null, | ||
lastName: token.uln || null, | ||
} | ||
: null, | ||
}; | ||
} | ||
exports.getChatiumContext = getChatiumContext; |
@@ -10,1 +10,21 @@ export declare abstract class ChatiumError extends Error { | ||
} | ||
export declare class AuthRequiredError extends ChatiumError { | ||
readonly statusCode = 401; | ||
constructor(reason?: string); | ||
} | ||
export declare class AccessDeniedError extends ChatiumError { | ||
readonly statusCode = 403; | ||
readonly type: string | undefined; | ||
readonly id: string | number | undefined; | ||
readonly action: string | undefined; | ||
readonly reason: string | undefined; | ||
constructor(message: string); | ||
constructor(type: string, id: string | number, action?: string, reason?: string); | ||
} | ||
export declare class ApiError extends ChatiumError { | ||
readonly statusCode = 400; | ||
} | ||
export declare class WrongArgumentError extends ChatiumError { | ||
readonly statusCode = 500; | ||
constructor(reason?: string); | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NotFoundError = exports.ChatiumError = void 0; | ||
exports.WrongArgumentError = exports.ApiError = exports.AccessDeniedError = exports.AuthRequiredError = exports.NotFoundError = exports.ChatiumError = void 0; | ||
class ChatiumError extends Error { | ||
@@ -16,1 +16,44 @@ } | ||
exports.NotFoundError = NotFoundError; | ||
class AuthRequiredError extends ChatiumError { | ||
constructor(reason) { | ||
super('Authentication required!' + (reason ? ' Reason: ' + reason : '')); | ||
this.statusCode = 401; | ||
} | ||
} | ||
exports.AuthRequiredError = AuthRequiredError; | ||
class AccessDeniedError extends ChatiumError { | ||
constructor(...args) { | ||
super(args.length === 0 | ||
? 'Access denied!' | ||
: args.length === 1 | ||
? args[0] | ||
: args.length === 2 | ||
? `Access denied to ${args[0]}[${args[1]}]` | ||
: args.length === 3 | ||
? `Access denied to ${args[0]}[${args[1]}] (${args[2]})` | ||
: `Access denied to ${args[0]}[${args[1]}] (${args[2]}). ` + | ||
`Reason: ${args[3]}`); | ||
this.statusCode = 403; | ||
if (args.length > 1) { | ||
this.type = args[0]; | ||
this.id = args[1]; | ||
this.action = args[2]; | ||
this.reason = args[3]; | ||
} | ||
} | ||
} | ||
exports.AccessDeniedError = AccessDeniedError; | ||
class ApiError extends ChatiumError { | ||
constructor() { | ||
super(...arguments); | ||
this.statusCode = 400; | ||
} | ||
} | ||
exports.ApiError = ApiError; | ||
class WrongArgumentError extends ChatiumError { | ||
constructor(reason) { | ||
super('Wrong argument error!' + (reason ? ' Reason: ' + reason : '')); | ||
this.statusCode = 500; | ||
} | ||
} | ||
exports.WrongArgumentError = WrongArgumentError; |
@@ -1,3 +0,4 @@ | ||
import type { AccountCtx, AppCtx, AuthCtx } from '../context'; | ||
import type { AccountCtx, AppCtx } from '../context'; | ||
import type { CreateFields, HeapData, HeapFieldMetas, HeapId, HeapObject, HeapObjectBase, HeapObjectType, UpdateFields } from './types'; | ||
import { AuthCtx } from '../ChatiumAuth'; | ||
declare type ReadHeapCtx = AccountCtx & AppCtx & AuthCtx; | ||
@@ -4,0 +5,0 @@ declare type WriteHeapCtx = AccountCtx & AppCtx & AuthCtx; |
@@ -1,4 +0,7 @@ | ||
import type { AccountCtx, AppCtx, AuthCtx } from './context'; | ||
export { chatiumGet, chatiumPost } from './api/chatiumApiClient'; | ||
export { AuthCtx, ChatiumAuth, ChatiumAuthType, OptionalAuthCtx } from './ChatiumAuth'; | ||
export { AccountCtx, AppCtx, ChatiumHeaders, getChatiumContext } from './context'; | ||
export { ChatiumUser, ChatiumUserRole, checkUserRoleAuthorization, OptionalUserCtx, UserCtx } from './ChatiumUser'; | ||
export { triggerHotReload } from './devTools'; | ||
export { AccessDeniedError, AuthRequiredError, ChatiumError, NotFoundError, WrongArgumentError } from './errors'; | ||
export { HeapRepo } from './heap/HeapRepo'; | ||
export { AccountCtx, AppCtx, AuthCtx }; |
@@ -6,3 +6,18 @@ "use strict"; | ||
Object.defineProperty(exports, "chatiumPost", { enumerable: true, get: function () { return chatiumApiClient_1.chatiumPost; } }); | ||
var ChatiumAuth_1 = require("./ChatiumAuth"); | ||
Object.defineProperty(exports, "ChatiumAuthType", { enumerable: true, get: function () { return ChatiumAuth_1.ChatiumAuthType; } }); | ||
var context_1 = require("./context"); | ||
Object.defineProperty(exports, "getChatiumContext", { enumerable: true, get: function () { return context_1.getChatiumContext; } }); | ||
var ChatiumUser_1 = require("./ChatiumUser"); | ||
Object.defineProperty(exports, "ChatiumUserRole", { enumerable: true, get: function () { return ChatiumUser_1.ChatiumUserRole; } }); | ||
Object.defineProperty(exports, "checkUserRoleAuthorization", { enumerable: true, get: function () { return ChatiumUser_1.checkUserRoleAuthorization; } }); | ||
var devTools_1 = require("./devTools"); | ||
Object.defineProperty(exports, "triggerHotReload", { enumerable: true, get: function () { return devTools_1.triggerHotReload; } }); | ||
var errors_1 = require("./errors"); | ||
Object.defineProperty(exports, "AccessDeniedError", { enumerable: true, get: function () { return errors_1.AccessDeniedError; } }); | ||
Object.defineProperty(exports, "AuthRequiredError", { enumerable: true, get: function () { return errors_1.AuthRequiredError; } }); | ||
Object.defineProperty(exports, "ChatiumError", { enumerable: true, get: function () { return errors_1.ChatiumError; } }); | ||
Object.defineProperty(exports, "NotFoundError", { enumerable: true, get: function () { return errors_1.NotFoundError; } }); | ||
Object.defineProperty(exports, "WrongArgumentError", { enumerable: true, get: function () { return errors_1.WrongArgumentError; } }); | ||
var HeapRepo_1 = require("./heap/HeapRepo"); | ||
Object.defineProperty(exports, "HeapRepo", { enumerable: true, get: function () { return HeapRepo_1.HeapRepo; } }); |
{ | ||
"name": "@chatium/sdk", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "SDK for developing custom chatium backends using NodeJS", | ||
@@ -25,3 +25,3 @@ "main": "lib/index.js", | ||
"post-rewrite": "yarnhook", | ||
"pre-commit": "npm run build:publish && lint-staged", | ||
"pre-commit": "npm run prepublishOnly && lint-staged", | ||
"post-commit": "git update-index --again" | ||
@@ -38,5 +38,5 @@ } | ||
"scripts": { | ||
"build:publish": "tsc --sourceMap false --declaration", | ||
"build:watch": "tsc -w", | ||
"clean": "rimraf lib" | ||
"clean": "rimraf lib", | ||
"prepublishOnly": "tsc --sourceMap false" | ||
}, | ||
@@ -43,0 +43,0 @@ "dependencies": { |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
33076
31
774
1