@botpress/api 0.7.2 → 0.8.0

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.
@@ -1004,6 +1004,7 @@ export declare const state: {
1004
1004
  expiry: {
1005
1005
  type: "number";
1006
1006
  minimum: number;
1007
+ maximum: number;
1007
1008
  description: string;
1008
1009
  };
1009
1010
  };
@@ -1571,7 +1572,6 @@ export declare const state: {
1571
1572
  required: string[];
1572
1573
  additionalProperties: false;
1573
1574
  };
1574
- description: string;
1575
1575
  };
1576
1576
  configuration: {
1577
1577
  type: "object";
@@ -5528,4 +5528,7 @@ export declare const state: {
5528
5528
  operations: string[];
5529
5529
  schema: string;
5530
5530
  })[];
5531
+ options: {
5532
+ allowUnions: false;
5533
+ };
5531
5534
  };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare const api: {
2
+ getState: () => import("@bpinternal/opapi").State<"Bot" | "Integration" | "Workspace" | "WorkspaceMember" | "Account" | "Usage" | "User" | "Conversation" | "Event" | "Message" | "State" | "File", "x-workspace-id", "user" | "conversation" | "event" | "message" | "file" | "state" | "hub" | "action" | "bot" | "integration" | "workspace" | "workspaceMember" | "account" | "usage">;
2
3
  getModelRef: (name: "Bot" | "Integration" | "Workspace" | "WorkspaceMember" | "Account" | "Usage" | "User" | "Conversation" | "Event" | "Message" | "State" | "File") => import("@bpinternal/opapi").OpenApiZodAny;
3
4
  addOperation: <Path extends string>(operationProps: import("@bpinternal/opapi").Operation<"x-workspace-id", "user" | "conversation" | "event" | "message" | "file" | "state" | "hub" | "action" | "bot" | "integration" | "workspace" | "workspaceMember" | "account" | "usage", Path, "zod-schema">) => void;
4
5
  exportClient: (dir: string | undefined, openapiGeneratorEndpoint: string, postProcessors?: import("@bpinternal/opapi").OpenApiPostProcessors | undefined) => Promise<void>;
package/dist/index.js CHANGED
@@ -68575,7 +68575,7 @@ var require_api2 = __commonJS({
68575
68575
  var QUOTE$1 = '"';
68576
68576
  var SLASH = "/";
68577
68577
  var STAR = "*";
68578
- var DEFAULT_OPTIONS = {
68578
+ var DEFAULT_OPTIONS2 = {
68579
68579
  comments: false,
68580
68580
  ranges: false
68581
68581
  };
@@ -68599,7 +68599,7 @@ var require_api2 = __commonJS({
68599
68599
  }
68600
68600
  function tokenize(text, options) {
68601
68601
  options = Object.freeze({
68602
- ...DEFAULT_OPTIONS,
68602
+ ...DEFAULT_OPTIONS2,
68603
68603
  ...options
68604
68604
  });
68605
68605
  let offset = -1;
@@ -258872,7 +258872,7 @@ __export(src_exports, {
258872
258872
  });
258873
258873
  module.exports = __toCommonJS(src_exports);
258874
258874
 
258875
- // ../../node_modules/.pnpm/@bpinternal+opapi@0.3.4_openapi-types@12.1.0/node_modules/@bpinternal/opapi/dist/index.mjs
258875
+ // ../../node_modules/.pnpm/@bpinternal+opapi@0.5.1_openapi-types@12.1.0/node_modules/@bpinternal/opapi/dist/index.mjs
258876
258876
  var import_zod_openapi = __toESM(require_src(), 1);
258877
258877
  var import_zod_openapi2 = __toESM(require_src(), 1);
258878
258878
  var import_chalk = __toESM(require_source(), 1);
@@ -258916,7 +258916,7 @@ var title = (str2) => {
258916
258916
  return str2.split(/(?=[A-Z])|[\.\-\s_]/).map((s) => s.trim()).filter((s) => !!s).map((s) => capitalize(s.toLowerCase())).join(" ");
258917
258917
  };
258918
258918
 
258919
- // ../../node_modules/.pnpm/@bpinternal+opapi@0.3.4_openapi-types@12.1.0/node_modules/@bpinternal/opapi/dist/index.mjs
258919
+ // ../../node_modules/.pnpm/@bpinternal+opapi@0.5.1_openapi-types@12.1.0/node_modules/@bpinternal/opapi/dist/index.mjs
258920
258920
  var import_decompress = __toESM(require_decompress(), 1);
258921
258921
  var import_fs2 = __toESM(require("fs"), 1);
258922
258922
  var import_promises = __toESM(require("fs/promises"), 1);
@@ -265905,7 +265905,7 @@ async function openapiTS(schema3, options = {}) {
265905
265905
  }
265906
265906
  var dist_default = openapiTS;
265907
265907
 
265908
- // ../../node_modules/.pnpm/@bpinternal+opapi@0.3.4_openapi-types@12.1.0/node_modules/@bpinternal/opapi/dist/index.mjs
265908
+ // ../../node_modules/.pnpm/@bpinternal+opapi@0.5.1_openapi-types@12.1.0/node_modules/@bpinternal/opapi/dist/index.mjs
265909
265909
  var import_zod_openapi3 = __toESM(require_src(), 1);
265910
265910
  var import_openapi3_ts = __toESM(require_dist3(), 1);
265911
265911
  var import_verror2 = __toESM(require_verror(), 1);
@@ -269500,7 +269500,7 @@ var z = /* @__PURE__ */ Object.freeze({
269500
269500
  ZodError
269501
269501
  });
269502
269502
 
269503
- // ../../node_modules/.pnpm/@bpinternal+opapi@0.3.4_openapi-types@12.1.0/node_modules/@bpinternal/opapi/dist/index.mjs
269503
+ // ../../node_modules/.pnpm/@bpinternal+opapi@0.5.1_openapi-types@12.1.0/node_modules/@bpinternal/opapi/dist/index.mjs
269504
269504
  var import_openapi_parser = __toESM(require_lib11(), 1);
269505
269505
  var import_json_schema_to_typescript = __toESM(require_src3(), 1);
269506
269506
  var import_zod_openapi4 = __toESM(require_src(), 1);
@@ -424918,7 +424918,7 @@ function generateErrors(errors) {
424918
424918
  const types2 = errors.map((error2) => error2.type);
424919
424919
  return `
424920
424920
  import crypto from 'crypto'
424921
-
424921
+
424922
424922
  const codes = {
424923
424923
  ${Object.entries(codes).map(([name, code]) => ` ${name}: ${code},`).join("\n")}
424924
424924
  } as const
@@ -424961,6 +424961,10 @@ abstract class BaseApiError<Code extends ErrorCode, Type extends string, Descrip
424961
424961
  }
424962
424962
  }
424963
424963
 
424964
+ format() {
424965
+ return \`[\${this.type}] \${this.message} (Error ID: \${this.id})\`
424966
+ }
424967
+
424964
424968
  toJSON() {
424965
424969
  return {
424966
424970
  id: this.id,
@@ -424969,7 +424973,7 @@ abstract class BaseApiError<Code extends ErrorCode, Type extends string, Descrip
424969
424973
  message: this.message,
424970
424974
  }
424971
424975
  }
424972
-
424976
+
424973
424977
  static generateId() {
424974
424978
  const randomSuffixByteLength = 4
424975
424979
  const randomHexSuffix = Array.from(cryptoLib.getRandomValues(new Uint8Array(randomSuffixByteLength)))
@@ -425012,7 +425016,7 @@ function getApiErrorFromObject(err: any) {
425012
425016
  if (!ErrorClass) {
425013
425017
  return new UnknownError(\`An unclassified API error occurred: \${err.message} (Type: \${err.type}, Code: \${err.code})\`)
425014
425018
  }
425015
-
425019
+
425016
425020
  return new ErrorClass(err.message, undefined, <string>err.id || 'UNKNOWN') // If error ID was not received do not pass undefined to generate a new one, flag it as UNKNOWN so we can fix the issue.
425017
425021
  }
425018
425022
 
@@ -425077,12 +425081,23 @@ function removeFromArray(array, item) {
425077
425081
  array.splice(index, 1);
425078
425082
  }
425079
425083
  }
425080
- var generateSchemaFromZod = (zodRef, useOutput) => {
425081
- const jsonSchema = (0, import_zod_openapi3.generateSchema)(zodRef, useOutput);
425082
- formatJsonSchema(jsonSchema);
425084
+ function uniqueBy(array, k) {
425085
+ const seen = /* @__PURE__ */ new Set();
425086
+ return array.filter((item) => {
425087
+ const v = item[k];
425088
+ if (seen.has(v)) {
425089
+ return false;
425090
+ }
425091
+ seen.add(v);
425092
+ return true;
425093
+ });
425094
+ }
425095
+ var generateSchemaFromZod = (zodRef, opts) => {
425096
+ const jsonSchema = (0, import_zod_openapi3.generateSchema)(zodRef, opts == null ? void 0 : opts.useOutput);
425097
+ formatJsonSchema(jsonSchema, (opts == null ? void 0 : opts.allowUnions) ?? false);
425083
425098
  return jsonSchema;
425084
425099
  };
425085
- var formatJsonSchema = (jsonSchema) => {
425100
+ var formatJsonSchema = (jsonSchema, allowUnions) => {
425086
425101
  if (jsonSchema.type === "object") {
425087
425102
  if (jsonSchema.additionalProperties === void 0) {
425088
425103
  jsonSchema.additionalProperties = false;
@@ -425094,11 +425109,11 @@ var formatJsonSchema = (jsonSchema) => {
425094
425109
  delete jsonSchema.required;
425095
425110
  }
425096
425111
  if (typeof jsonSchema.additionalProperties === "object") {
425097
- formatJsonSchema(jsonSchema.additionalProperties);
425112
+ formatJsonSchema(jsonSchema.additionalProperties, allowUnions);
425098
425113
  }
425099
- Object.entries(jsonSchema.properties ?? {}).forEach(([_, value]) => formatJsonSchema(value));
425114
+ Object.entries(jsonSchema.properties ?? {}).forEach(([_, value]) => formatJsonSchema(value, allowUnions));
425100
425115
  }
425101
- if (jsonSchema.allOf || jsonSchema.anyOf || jsonSchema.oneOf) {
425116
+ if (!allowUnions && (jsonSchema.allOf || jsonSchema.anyOf || jsonSchema.oneOf)) {
425102
425117
  throw new Error("allOf, anyOf and oneOf are not supported");
425103
425118
  }
425104
425119
  };
@@ -425125,6 +425140,7 @@ var objects;
425125
425140
  return result;
425126
425141
  };
425127
425142
  })(objects || (objects = {}));
425143
+ var DEFAULT_OPTIONS = { allowUnions: false };
425128
425144
  var unknownError = {
425129
425145
  status: 500,
425130
425146
  type: "Unknown",
@@ -425142,7 +425158,8 @@ function isOperationWithBodyProps(operation) {
425142
425158
  } else
425143
425159
  return false;
425144
425160
  }
425145
- function createState(props) {
425161
+ function createState(props, opts = {}) {
425162
+ const options = { ...DEFAULT_OPTIONS, ...opts };
425146
425163
  const schemaEntries = props.schemas ? Object.entries(props.schemas).map(([name, data]) => ({
425147
425164
  name,
425148
425165
  schema: data.schema,
@@ -425175,17 +425192,14 @@ function createState(props) {
425175
425192
  }
425176
425193
  schemas[name] = {
425177
425194
  section: schemaEntry.section,
425178
- schema: generateSchemaFromZod(schemaEntry.schema)
425195
+ schema: generateSchemaFromZod(schemaEntry.schema, options)
425179
425196
  };
425180
425197
  refs.schemas[name] = true;
425181
425198
  });
425182
- const errors = [unknownError, internalError, ...props.errors ?? []];
425183
- const existingErrors = /* @__PURE__ */ new Set();
425199
+ const userErrors = props.errors ?? [];
425200
+ const defaultErrors = [unknownError, internalError];
425201
+ const errors = uniqueBy([...defaultErrors, ...userErrors], "type");
425184
425202
  errors.forEach((error2) => {
425185
- if (existingErrors.has(error2.type)) {
425186
- throw new import_verror3.VError(`Error ${error2.type} already exists`);
425187
- }
425188
- existingErrors.add(error2.type);
425189
425203
  if (!isCapitalAlphabetical(error2.type)) {
425190
425204
  throw new import_verror3.VError(`Invalid error type ${error2.type}. It must be alphabetical and start with a capital letter`);
425191
425205
  }
@@ -425204,7 +425218,8 @@ function createState(props) {
425204
425218
  errors,
425205
425219
  refs,
425206
425220
  schemas,
425207
- sections
425221
+ sections,
425222
+ options
425208
425223
  };
425209
425224
  }
425210
425225
  function getRef(state2, type2, name) {
@@ -425397,6 +425412,16 @@ var composeFilesFromBlocks = (blocks, targetDirectory) => {
425397
425412
  }
425398
425413
  });
425399
425414
  };
425415
+ var generateSectionsFile = (state2, targetDirectory) => {
425416
+ log_default.info("Generating sections file");
425417
+ const sections = state2.sections.map((section) => section.name);
425418
+ saveFile(
425419
+ targetDirectory,
425420
+ "sections.ts",
425421
+ `export const sections = ${JSON.stringify(sections)} as const;
425422
+ export type Sections = typeof sections[number];`
425423
+ );
425424
+ };
425400
425425
  var getImportsForDependencies = (block, blocks) => {
425401
425426
  let content = "";
425402
425427
  if (Boolean(block.dependencies.length)) {
@@ -425574,6 +425599,7 @@ async function generateTypesBySection(state2, targetDirectory) {
425574
425599
  allBlocks.push(...blocks);
425575
425600
  }
425576
425601
  composeFilesFromBlocks(allBlocks, targetDirectory);
425602
+ generateSectionsFile(state2, targetDirectory);
425577
425603
  }
425578
425604
  var generateServer = async (state2, dir, useExpressTypes) => {
425579
425605
  initDirectory(dir);
@@ -425740,7 +425766,7 @@ var addOperation = (state2, operationProps) => {
425740
425766
  const response = {
425741
425767
  description: operationProps.response.description,
425742
425768
  status: operationProps.response.status,
425743
- schema: generateSchemaFromZod((0, import_zod_openapi4.extendApi)(operationProps.response.schema, { title: responseName }))
425769
+ schema: generateSchemaFromZod((0, import_zod_openapi4.extendApi)(operationProps.response.schema, { title: responseName }), state2.options)
425744
425770
  };
425745
425771
  let operation;
425746
425772
  if (isOperationWithBodyProps(operationProps)) {
@@ -425752,7 +425778,7 @@ var addOperation = (state2, operationProps) => {
425752
425778
  response,
425753
425779
  requestBody: {
425754
425780
  description: operationProps.requestBody.description,
425755
- schema: generateSchemaFromZod((0, import_zod_openapi4.extendApi)(operationProps.requestBody.schema, { title: bodyName }))
425781
+ schema: generateSchemaFromZod((0, import_zod_openapi4.extendApi)(operationProps.requestBody.schema, { title: bodyName }), state2.options)
425756
425782
  }
425757
425783
  };
425758
425784
  } else {
@@ -425836,6 +425862,7 @@ var exportStateAsTypescript = (state2, dir) => {
425836
425862
  var schema2 = import_zod_openapi2.extendApi;
425837
425863
  var createOpapiFromState = (state2) => {
425838
425864
  return {
425865
+ getState: () => state2,
425839
425866
  getModelRef: (name) => getRef(state2, "schemas", name),
425840
425867
  addOperation: (operationProps) => addOperation(state2, operationProps),
425841
425868
  exportClient: (dir = ".", openapiGeneratorEndpoint, postProcessors) => generateClient(state2, dir, openapiGeneratorEndpoint, postProcessors),
@@ -425846,8 +425873,8 @@ var createOpapiFromState = (state2) => {
425846
425873
  exportErrors: (dir = ".") => generateErrorsFile(state2.errors ?? [], dir)
425847
425874
  };
425848
425875
  };
425849
- function OpenApi(props) {
425850
- const state2 = createState(props);
425876
+ function OpenApi(props, opts = {}) {
425877
+ const state2 = createState(props, opts);
425851
425878
  return createOpapiFromState(state2);
425852
425879
  }
425853
425880
  ((OpenApi2) => {
@@ -426861,6 +426888,7 @@ var state = {
426861
426888
  expiry: {
426862
426889
  type: "number",
426863
426890
  minimum: 1,
426891
+ maximum: 2592e6,
426864
426892
  description: "Expiry of the [State](#schema_state) in milliseconds. The state will expire if it is idle for the configured value. By default, a state doesn't expire."
426865
426893
  }
426866
426894
  },
@@ -427436,8 +427464,7 @@ var state = {
427436
427464
  },
427437
427465
  required: ["schedule", "type", "payload"],
427438
427466
  additionalProperties: false
427439
- },
427440
- description: "Recurring events"
427467
+ }
427441
427468
  },
427442
427469
  configuration: {
427443
427470
  type: "object",
@@ -428349,7 +428376,8 @@ var state = {
428349
428376
  "knowledgebase_vector_count",
428350
428377
  "bot_ratelimit",
428351
428378
  "table_row_count",
428352
- "workspace_member_count"
428379
+ "workspace_member_count",
428380
+ "integrations_owned_count"
428353
428381
  ],
428354
428382
  required: true
428355
428383
  },
@@ -428405,7 +428433,8 @@ var state = {
428405
428433
  "knowledgebase_vector_count",
428406
428434
  "bot_ratelimit",
428407
428435
  "table_row_count",
428408
- "workspace_member_count"
428436
+ "workspace_member_count",
428437
+ "integrations_owned_count"
428409
428438
  ],
428410
428439
  required: true
428411
428440
  },
@@ -428442,7 +428471,8 @@ var state = {
428442
428471
  "knowledgebase_vector_count",
428443
428472
  "bot_ratelimit",
428444
428473
  "table_row_count",
428445
- "workspace_member_count"
428474
+ "workspace_member_count",
428475
+ "integrations_owned_count"
428446
428476
  ],
428447
428477
  description: "Usage type that can be used"
428448
428478
  }
@@ -428507,7 +428537,8 @@ var state = {
428507
428537
  "knowledgebase_vector_count",
428508
428538
  "bot_ratelimit",
428509
428539
  "table_row_count",
428510
- "workspace_member_count"
428540
+ "workspace_member_count",
428541
+ "integrations_owned_count"
428511
428542
  ],
428512
428543
  description: "Usage type that can be used"
428513
428544
  }
@@ -430044,7 +430075,8 @@ var state = {
430044
430075
  "knowledgebase_vector_count",
430045
430076
  "bot_ratelimit",
430046
430077
  "table_row_count",
430047
- "workspace_member_count"
430078
+ "workspace_member_count",
430079
+ "integrations_owned_count"
430048
430080
  ],
430049
430081
  required: true
430050
430082
  },
@@ -430093,7 +430125,8 @@ var state = {
430093
430125
  "knowledgebase_vector_count",
430094
430126
  "bot_ratelimit",
430095
430127
  "table_row_count",
430096
- "workspace_member_count"
430128
+ "workspace_member_count",
430129
+ "integrations_owned_count"
430097
430130
  ],
430098
430131
  required: true
430099
430132
  },
@@ -430339,7 +430372,7 @@ var state = {
430339
430372
  title: "Botpress API",
430340
430373
  description: "API for Botpress Cloud",
430341
430374
  server: "https://api.botpress.cloud",
430342
- version: "0.7.2",
430375
+ version: "0.8.0",
430343
430376
  prefix: "v1"
430344
430377
  },
430345
430378
  errors: [
@@ -431381,7 +431414,8 @@ var state = {
431381
431414
  "knowledgebase_vector_count",
431382
431415
  "bot_ratelimit",
431383
431416
  "table_row_count",
431384
- "workspace_member_count"
431417
+ "workspace_member_count",
431418
+ "integrations_owned_count"
431385
431419
  ],
431386
431420
  description: "Usage type that can be used"
431387
431421
  }
@@ -431823,7 +431857,10 @@ var state = {
431823
431857
  operations: ["getUsage"],
431824
431858
  schema: "Usage"
431825
431859
  }
431826
- ]
431860
+ ],
431861
+ options: {
431862
+ allowUnions: false
431863
+ }
431827
431864
  };
431828
431865
 
431829
431866
  // src/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botpress/api",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -22,6 +22,6 @@
22
22
  "zod": "^3.20.6"
23
23
  },
24
24
  "dependencies": {
25
- "@bpinternal/opapi": "0.3.4"
25
+ "@bpinternal/opapi": "^0.5.1"
26
26
  }
27
27
  }