rads-db 3.0.21 → 3.0.23

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.
@@ -4,11 +4,39 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getRadsTunnelHandler = getRadsTunnelHandler;
7
+ exports.getRadsUiUser = getRadsUiUser;
7
8
  exports.getRestRoutes = getRestRoutes;
8
9
  var _h = require("h3");
9
10
  var _lodash = _interopRequireDefault(require("lodash"));
10
11
  var _restEndpointsDev = _interopRequireDefault(require("./restEndpointsDev.cjs"));
11
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+ function getRadsUiUser(radsAuthHeader, apiKey) {
14
+ if (!radsAuthHeader) return void 0;
15
+ apiKey = apiKey || process.env.RadsApiKey;
16
+ if (!apiKey) {
17
+ throw (0, _h.createError)({
18
+ statusCode: 401,
19
+ message: `Please, provide "apiKey" option to "getRadsUiUser" call.`
20
+ });
21
+ }
22
+ const [keyType, key, email, pretendAsUserId] = radsAuthHeader.split(" ");
23
+ if (!email || !keyType || !key || keyType !== "ApiKey") {
24
+ throw (0, _h.createError)({
25
+ statusCode: 401,
26
+ message: `Unauthorized - malformed authHeader`
27
+ });
28
+ }
29
+ if (apiKey !== key) {
30
+ throw (0, _h.createError)({
31
+ statusCode: 401,
32
+ message: `Unauthorized`
33
+ });
34
+ }
35
+ return {
36
+ email,
37
+ pretendAsUserId
38
+ };
39
+ }
12
40
  function getRestRoutes(options) {
13
41
  const {
14
42
  db,
@@ -73,18 +101,10 @@ function getBlobFromDataUrl(dataUrl) {
73
101
  function getRadsTunnelHandler(options) {
74
102
  const db = options.db;
75
103
  const exposeFilesystem = options.exposeFilesystem ?? process.env.NODE_ENV === "development";
76
- return async function radsTunnelHandler(request, ctx) {
77
- const {
78
- method,
79
- entity,
80
- args,
104
+ const handlers = {
105
+ uploadFile: ({
81
106
  uploadArgs
82
- } = request?.body || {};
83
- if (!method) throw (0, _h.createError)({
84
- statusCode: 400,
85
- message: "Malformed request - missing method"
86
- });
87
- if (method === "uploadFile") {
107
+ }, ctx) => {
88
108
  const blob = getBlobFromDataUrl(uploadArgs?.blobDataUrl);
89
109
  if (!blob) throw (0, _h.createError)({
90
110
  statusCode: 400,
@@ -94,16 +114,24 @@ function getRadsTunnelHandler(options) {
94
114
  blob,
95
115
  ...uploadArgs
96
116
  }, ctx);
97
- }
98
- if (method === "_schema") {
99
- return db._schema;
100
- }
101
- if (method === "_radsUiSlots") {
102
- return db._radsUiSlots;
103
- }
104
- if (method === "getProcessInfo") {
105
- return {};
106
- }
117
+ },
118
+ _schema: () => db._schema,
119
+ // @ts-expect-error for internal usage. don't want to expose this property in types
120
+ _radsUiSlots: () => db._radsUiSlots,
121
+ getProcessInfo: () => ({})
122
+ };
123
+ return async function radsTunnelHandler(request, ctx) {
124
+ const {
125
+ method,
126
+ entity,
127
+ args
128
+ } = request?.body || {};
129
+ if (!method) throw (0, _h.createError)({
130
+ statusCode: 400,
131
+ message: "Malformed request - missing method"
132
+ });
133
+ const handler = handlers[method];
134
+ if (handler) return handler(request?.body || {}, ctx);
107
135
  if (exposeFilesystem && method.startsWith("fs:")) {
108
136
  const fsMethod = method.slice(3);
109
137
  const devHandler = _restEndpointsDev.default[fsMethod];
@@ -1,3 +1,7 @@
1
1
  import type { GetRestRoutesArgs, GetRestRoutesOptions, GetRestRoutesResponse, RadsRequestContext } from '../types';
2
+ export declare function getRadsUiUser(radsAuthHeader?: string, apiKey?: string): {
3
+ email: string;
4
+ pretendAsUserId: string;
5
+ } | undefined;
2
6
  export declare function getRestRoutes(options: GetRestRoutesOptions): GetRestRoutesResponse;
3
7
  export declare function getRadsTunnelHandler(options: GetRestRoutesOptions): (request: GetRestRoutesArgs, ctx?: RadsRequestContext) => Promise<any>;
@@ -1,6 +1,22 @@
1
1
  import { createError } from "h3";
2
2
  import _ from "lodash";
3
3
  import restEndpointsDev from "./restEndpointsDev.mjs";
4
+ export function getRadsUiUser(radsAuthHeader, apiKey) {
5
+ if (!radsAuthHeader)
6
+ return void 0;
7
+ apiKey = apiKey || process.env.RadsApiKey;
8
+ if (!apiKey) {
9
+ throw createError({ statusCode: 401, message: `Please, provide "apiKey" option to "getRadsUiUser" call.` });
10
+ }
11
+ const [keyType, key, email, pretendAsUserId] = radsAuthHeader.split(" ");
12
+ if (!email || !keyType || !key || keyType !== "ApiKey") {
13
+ throw createError({ statusCode: 401, message: `Unauthorized - malformed authHeader` });
14
+ }
15
+ if (apiKey !== key) {
16
+ throw createError({ statusCode: 401, message: `Unauthorized` });
17
+ }
18
+ return { email, pretendAsUserId };
19
+ }
4
20
  export function getRestRoutes(options) {
5
21
  const { db, prefix = "/api/" } = options;
6
22
  const routes = {};
@@ -52,25 +68,25 @@ function getBlobFromDataUrl(dataUrl) {
52
68
  export function getRadsTunnelHandler(options) {
53
69
  const db = options.db;
54
70
  const exposeFilesystem = options.exposeFilesystem ?? process.env.NODE_ENV === "development";
55
- return async function radsTunnelHandler(request, ctx) {
56
- const { method, entity, args, uploadArgs } = request?.body || {};
57
- if (!method)
58
- throw createError({ statusCode: 400, message: "Malformed request - missing method" });
59
- if (method === "uploadFile") {
71
+ const handlers = {
72
+ uploadFile: ({ uploadArgs }, ctx) => {
60
73
  const blob = getBlobFromDataUrl(uploadArgs?.blobDataUrl);
61
74
  if (!blob)
62
75
  throw createError({ statusCode: 400, message: 'Malformed request - "blobDataUrl" is required' });
63
76
  return db.uploadFile({ blob, ...uploadArgs }, ctx);
64
- }
65
- if (method === "_schema") {
66
- return db._schema;
67
- }
68
- if (method === "_radsUiSlots") {
69
- return db._radsUiSlots;
70
- }
71
- if (method === "getProcessInfo") {
72
- return {};
73
- }
77
+ },
78
+ _schema: () => db._schema,
79
+ // @ts-expect-error for internal usage. don't want to expose this property in types
80
+ _radsUiSlots: () => db._radsUiSlots,
81
+ getProcessInfo: () => ({})
82
+ };
83
+ return async function radsTunnelHandler(request, ctx) {
84
+ const { method, entity, args } = request?.body || {};
85
+ if (!method)
86
+ throw createError({ statusCode: 400, message: "Malformed request - missing method" });
87
+ const handler = handlers[method];
88
+ if (handler)
89
+ return handler(request?.body || {}, ctx);
74
90
  if (exposeFilesystem && method.startsWith("fs:")) {
75
91
  const fsMethod = method.slice(3);
76
92
  const devHandler = restEndpointsDev[fsMethod];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rads-db",
3
- "version": "3.0.21",
3
+ "version": "3.0.23",
4
4
  "files": [
5
5
  "dist",
6
6
  "drivers",