@internetderdinge/api 1.229.26 → 1.229.28

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/src/index.js CHANGED
@@ -23,7 +23,7 @@ export { default as devicesNotificationsRoute } from "./devicesNotifications/dev
23
23
  export { default as devicesNotificationsService } from "../src/devicesNotifications/devicesNotifications.service";
24
24
  export { default as iotDevicesService } from "../src/iotdevice/iotdevice.service";
25
25
  export { default as iotdeviceRoute } from "../src/iotdevice/iotdevice.route";
26
- export { SIMILARITY_THRESHOLD } from "../src/iotdevice/iotdevice.service";
26
+ export { SIMILARITY_THRESHOLD, alarmsDevice, getDeviceStatus, getDeviceStatusList, shadowAlarmGet, shadowAlarmUpdate, } from "../src/iotdevice/iotdevice.service";
27
27
  export { default as pdfRoute } from "../src/pdf/pdf.route";
28
28
  export { default as tokensRoute } from "../src/tokens/tokens.route";
29
29
  export * from "../src/tokens/tokens.service";
@@ -7,7 +7,7 @@ import { getEventsSchema, getDeviceSchema, getEntrySchema, updateEntrySchema, pi
7
7
  // add other input schemas as needed
8
8
  } from "./iotdevice.validation";
9
9
  import { iotDeviceResponseSchema, eventResponseSchema, deviceResponseSchema, shadowAlarmSchema, pingResponseSchema, deviceStatusSchema, apiStatusSchema, entryResponseSchema, } from "./iotdevice.schemas";
10
- import { getIotDevices, getEvents, getDevice, shadowAlarmGet, shadowAlarmUpdate, shadowAdmin, pingDevice, getDeviceStatus, getApiStatus, getEntry, updateEntry, } from "./iotdevice.controller";
10
+ import { getIotDevices, getEvents, getDevice, shadowAlarmGet, shadowAlarmUpdate, shadowAdmin, pingDevice, getDeviceStatus, getApiStatus, getEntry, updateEntry, ledLightHint, } from "./iotdevice.controller";
11
11
  export const iotdeviceRouteSpecs = [
12
12
  {
13
13
  method: "get",
@@ -74,7 +74,7 @@ export const iotdeviceRouteSpecs = [
74
74
  path: "/ledlight/:deviceId",
75
75
  validate: [auth("getUsers"), validateAdmin],
76
76
  responseSchema: pingResponseSchema,
77
- handler: pingDevice,
77
+ handler: ledLightHint,
78
78
  summary: "Ping device LED light",
79
79
  description: "Sends a ping to the device’s LED light to test its connectivity or response.",
80
80
  },
@@ -310,7 +310,7 @@ export const ledLightHint = async (deviceName, body) => {
310
310
  * @param {Object} userBody
311
311
  * @returns {Promise<Device>}
312
312
  */
313
- const shadowAlarmUpdate = async (deviceName, alarms, shadowName) => {
313
+ export const shadowAlarmUpdate = async (deviceName, alarms, shadowName) => {
314
314
  const data = alarms;
315
315
  if (!deviceName) {
316
316
  return { error: "no deviceId" };
@@ -10,22 +10,19 @@ function hasRoleValidation(validators = []) {
10
10
  }
11
11
  export default function buildAiRouterAndDocs(router, routeSpecs, basePath = "/", tags = []) {
12
12
  routeSpecs.forEach((spec) => {
13
- // mount Express
14
- if (!spec.validate) {
15
- spec.validate = [];
13
+ const validate = spec.validate || [];
14
+ const routeMiddleware = spec.validateWithRequestSchema ||
15
+ (spec.requestSchema
16
+ ? [validateZod(spec.requestSchema), ...validate]
17
+ : validate);
18
+ router[spec.method](spec.path, ...routeMiddleware, spec.handler);
19
+ const { body, ...rest } = spec.requestSchema || {};
20
+ const request = { ...rest };
21
+ if (spec.requestBody) {
22
+ request.body = spec.requestBody;
16
23
  }
17
- if (spec.requestSchema) {
18
- spec.validateWithRequestSchema = [
19
- validateZod(spec.requestSchema),
20
- ...spec.validate,
21
- ];
22
- }
23
- if (spec.validateWithRequestSchema) {
24
- router[spec.method](spec.path, ...spec.validateWithRequestSchema, spec.handler);
25
- }
26
- var { body, ...rest } = spec.requestSchema || {};
27
- if (body) {
28
- rest.body = {
24
+ else if (body) {
25
+ request.body = {
29
26
  content: {
30
27
  "application/json": {
31
28
  schema: body,
@@ -34,17 +31,17 @@ export default function buildAiRouterAndDocs(router, routeSpecs, basePath = "/",
34
31
  };
35
32
  }
36
33
  if (spec.responseSchema &&
37
- !hasRoleValidation(spec.validate) &&
34
+ !hasRoleValidation(spec.validateWithRequestSchema || validate) &&
38
35
  spec.privateDocs !== true &&
39
36
  spec.memoOnly !== true) {
40
37
  // collect all middleware fn names (falls back to '<anonymous>' if unnamed)
41
- const middlewareNames = (spec.validate || []).map((fn) => `\`${fn.name}\`` || "<anonymous>");
38
+ const middlewareNames = (spec.validateWithRequestSchema || validate).map((fn) => `\`${fn.name}\`` || "<anonymous>");
42
39
  const openApiPath = (basePath + spec.path).replace(/:([A-Za-z0-9_]+)/g, "{$1}");
43
40
  registry.registerPath({
44
41
  method: spec.method,
45
42
  path: openApiPath,
46
43
  summary: spec.summary,
47
- request: rest,
44
+ request,
48
45
  // append middleware names to the description
49
46
  description: [
50
47
  spec.description,
@@ -3,6 +3,12 @@ import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
3
3
  import { z } from "zod";
4
4
  extendZodWithOpenApi(z);
5
5
  export const registry = new OpenAPIRegistry();
6
+ export const xApiKey = registry.registerComponent("securitySchemes", "x-api-key", {
7
+ type: "apiKey",
8
+ in: "header",
9
+ name: "x-api-key",
10
+ description: "API key for authentication",
11
+ });
6
12
  // add Bearer JWT auth
7
13
  export const bearerAuth = registry.registerComponent("securitySchemes", "bearerAuth", {
8
14
  type: "http",
@@ -10,12 +16,6 @@ export const bearerAuth = registry.registerComponent("securitySchemes", "bearerA
10
16
  bearerFormat: "JWT",
11
17
  description: "JWT Bearer authentication",
12
18
  });
13
- export const xApiKey = registry.registerComponent("securitySchemes", "x-api-key", {
14
- type: "apiKey",
15
- in: "header",
16
- name: "x-api-key",
17
- description: "API key for authentication",
18
- });
19
19
  const UserSchema = z
20
20
  .object({
21
21
  id: z.string().openapi({ example: "1212121" }),
@@ -23,45 +23,50 @@ const UserSchema = z
23
23
  age: z.number().openapi({ example: 42 }),
24
24
  })
25
25
  .openapi("User");
26
+ /*
26
27
  registry.registerPath({
27
- method: "get",
28
- path: "/usersnnn/{id}",
29
- summary: "Get a single user",
30
- request: {
31
- params: z.object({ id: z.string() }),
32
- },
33
- responses: {
34
- 200: {
35
- description: "Object with user data.",
36
- content: {
37
- "application/json": {
38
- schema: UserSchema,
39
- },
40
- },
28
+ method: "get",
29
+ path: "/usersnnn/{id}",
30
+ summary: "Get a single user",
31
+ request: {
32
+ params: z.object({ id: z.string() }),
33
+ },
34
+
35
+ responses: {
36
+ 200: {
37
+ description: "Object with user data.",
38
+ content: {
39
+ "application/json": {
40
+ schema: UserSchema,
41
41
  },
42
+ },
42
43
  },
44
+ },
43
45
  });
46
+
47
+
44
48
  registry.registerPath({
45
- method: "get",
46
- path: "/users/{id}",
47
- description: "Get user data by its id",
48
- summary: "Get a single user",
49
- request: {
50
- params: z.object({
51
- id: z.string().openapi({ example: "1212121" }),
52
- }),
53
- },
54
- responses: {
55
- 200: {
56
- description: "Object with user data.",
57
- content: {
58
- "application/json": {
59
- schema: UserSchema,
60
- },
61
- },
62
- },
63
- 204: {
64
- description: "No content - successful operation",
49
+ method: "get",
50
+ path: "/users/{id}",
51
+ description: "Get user data by its id",
52
+ summary: "Get a single user",
53
+ request: {
54
+ params: z.object({
55
+ id: z.string().openapi({ example: "1212121" }),
56
+ }),
57
+ },
58
+ responses: {
59
+ 200: {
60
+ description: "Object with user data.",
61
+ content: {
62
+ "application/json": {
63
+ schema: UserSchema,
65
64
  },
65
+ },
66
+ },
67
+ 204: {
68
+ description: "No content - successful operation",
66
69
  },
70
+ },
67
71
  });
72
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetderdinge/api",
3
- "version": "1.229.26",
3
+ "version": "1.229.28",
4
4
  "description": "Shared OpenIoT API modules",
5
5
  "main": "dist/src/index.js",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -28,7 +28,14 @@ export { default as devicesNotificationsRoute } from "./devicesNotifications/dev
28
28
  export { default as devicesNotificationsService } from "../src/devicesNotifications/devicesNotifications.service";
29
29
  export { default as iotDevicesService } from "../src/iotdevice/iotdevice.service";
30
30
  export { default as iotdeviceRoute } from "../src/iotdevice/iotdevice.route";
31
- export { SIMILARITY_THRESHOLD } from "../src/iotdevice/iotdevice.service";
31
+ export {
32
+ SIMILARITY_THRESHOLD,
33
+ alarmsDevice,
34
+ getDeviceStatus,
35
+ getDeviceStatusList,
36
+ shadowAlarmGet,
37
+ shadowAlarmUpdate,
38
+ } from "../src/iotdevice/iotdevice.service";
32
39
  export { default as pdfRoute } from "../src/pdf/pdf.route";
33
40
  export { default as tokensRoute } from "../src/tokens/tokens.route";
34
41
  export * from "../src/tokens/tokens.service";
@@ -40,6 +40,7 @@ import {
40
40
  getApiStatus,
41
41
  getEntry,
42
42
  updateEntry,
43
+ ledLightHint,
43
44
  } from "./iotdevice.controller";
44
45
  import { request } from "https";
45
46
 
@@ -113,7 +114,7 @@ export const iotdeviceRouteSpecs: RouteSpec[] = [
113
114
  path: "/ledlight/:deviceId",
114
115
  validate: [auth("getUsers"), validateAdmin],
115
116
  responseSchema: pingResponseSchema,
116
- handler: pingDevice,
117
+ handler: ledLightHint,
117
118
  summary: "Ping device LED light",
118
119
  description:
119
120
  "Sends a ping to the device’s LED light to test its connectivity or response.",
@@ -378,7 +378,7 @@ export const ledLightHint = async (deviceName, body) => {
378
378
  * @param {Object} userBody
379
379
  * @returns {Promise<Device>}
380
380
  */
381
- const shadowAlarmUpdate = async (deviceName, alarms, shadowName) => {
381
+ export const shadowAlarmUpdate = async (deviceName, alarms, shadowName) => {
382
382
  const data = alarms;
383
383
 
384
384
  if (!deviceName) {
@@ -21,6 +21,10 @@ export type RouteSpec = {
21
21
  validate?: RequestHandler<any, any, any, any, any>[];
22
22
  validateWithRequestSchema?: RequestHandler<any, any, any, any, any>[];
23
23
  requestSchema?: Partial<Record<string, ZodTypeAny>>;
24
+ requestBody?: {
25
+ required?: boolean;
26
+ content: Record<string, { schema: ZodTypeAny }>;
27
+ };
24
28
  responseSchema?: ZodTypeAny;
25
29
  handler: RequestHandler<any, any, any, any, any>;
26
30
  summary: string;
@@ -36,31 +40,22 @@ export default function buildAiRouterAndDocs(
36
40
  tags: string[] = [],
37
41
  ) {
38
42
  routeSpecs.forEach((spec) => {
39
- // mount Express
40
-
41
- if (!spec.validate) {
42
- spec.validate = [];
43
- }
44
-
45
- if (spec.requestSchema) {
46
- spec.validateWithRequestSchema = [
47
- validateZod(spec.requestSchema),
48
- ...spec.validate,
49
- ];
50
- }
51
-
52
- if (spec.validateWithRequestSchema) {
53
- router[spec.method](
54
- spec.path,
55
- ...spec.validateWithRequestSchema,
56
- spec.handler,
57
- );
58
- }
59
-
60
- var { body, ...rest } = spec.requestSchema || {};
61
-
62
- if (body) {
63
- rest.body = {
43
+ const validate = spec.validate || [];
44
+ const routeMiddleware =
45
+ spec.validateWithRequestSchema ||
46
+ (spec.requestSchema
47
+ ? [validateZod(spec.requestSchema), ...validate]
48
+ : validate);
49
+
50
+ router[spec.method](spec.path, ...routeMiddleware, spec.handler);
51
+
52
+ const { body, ...rest } = spec.requestSchema || {};
53
+ const request = { ...rest } as Record<string, unknown>;
54
+
55
+ if (spec.requestBody) {
56
+ request.body = spec.requestBody;
57
+ } else if (body) {
58
+ request.body = {
64
59
  content: {
65
60
  "application/json": {
66
61
  schema: body,
@@ -71,12 +66,12 @@ export default function buildAiRouterAndDocs(
71
66
 
72
67
  if (
73
68
  spec.responseSchema &&
74
- !hasRoleValidation(spec.validate) &&
69
+ !hasRoleValidation(spec.validateWithRequestSchema || validate) &&
75
70
  spec.privateDocs !== true &&
76
71
  spec.memoOnly !== true
77
72
  ) {
78
73
  // collect all middleware fn names (falls back to '<anonymous>' if unnamed)
79
- const middlewareNames = (spec.validate || []).map(
74
+ const middlewareNames = (spec.validateWithRequestSchema || validate).map(
80
75
  (fn) => `\`${fn.name}\`` || "<anonymous>",
81
76
  );
82
77
  const openApiPath = (basePath + spec.path).replace(
@@ -88,7 +83,7 @@ export default function buildAiRouterAndDocs(
88
83
  method: spec.method,
89
84
  path: openApiPath,
90
85
  summary: spec.summary,
91
- request: rest,
86
+ request,
92
87
 
93
88
  // append middleware names to the description
94
89
  description: [
@@ -6,6 +6,17 @@ extendZodWithOpenApi(z);
6
6
 
7
7
  export const registry = new OpenAPIRegistry();
8
8
 
9
+ export const xApiKey = registry.registerComponent(
10
+ "securitySchemes",
11
+ "x-api-key",
12
+ {
13
+ type: "apiKey",
14
+ in: "header",
15
+ name: "x-api-key",
16
+ description: "API key for authentication",
17
+ },
18
+ );
19
+
9
20
  // add Bearer JWT auth
10
21
  export const bearerAuth = registry.registerComponent(
11
22
  "securitySchemes",
@@ -18,17 +29,6 @@ export const bearerAuth = registry.registerComponent(
18
29
  },
19
30
  );
20
31
 
21
- export const xApiKey = registry.registerComponent(
22
- "securitySchemes",
23
- "x-api-key",
24
- {
25
- type: "apiKey",
26
- in: "header",
27
- name: "x-api-key",
28
- description: "API key for authentication",
29
- },
30
- );
31
-
32
32
  const UserSchema = z
33
33
  .object({
34
34
  id: z.string().openapi({ example: "1212121" }),
@@ -37,6 +37,7 @@ const UserSchema = z
37
37
  })
38
38
  .openapi("User");
39
39
 
40
+ /*
40
41
  registry.registerPath({
41
42
  method: "get",
42
43
  path: "/usersnnn/{id}",
@@ -57,6 +58,7 @@ registry.registerPath({
57
58
  },
58
59
  });
59
60
 
61
+
60
62
  registry.registerPath({
61
63
  method: "get",
62
64
  path: "/users/{id}",
@@ -81,3 +83,4 @@ registry.registerPath({
81
83
  },
82
84
  },
83
85
  });
86
+ */