@kadoa/node-sdk 0.12.0 → 0.13.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.
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ import globalAxios5, { isAxiosError, AxiosError } from 'axios';
2
2
  import { v4 } from 'uuid';
3
3
  import { URL, URLSearchParams } from 'url';
4
4
  import createDebug from 'debug';
5
- import { merge } from 'es-toolkit';
5
+ import { upperFirst, camelCase, merge } from 'es-toolkit';
6
6
  import { z } from 'zod';
7
7
  import assert from 'assert';
8
8
 
@@ -18,7 +18,9 @@ var KadoaErrorCode = {
18
18
  AUTH_ERROR: "AUTH_ERROR",
19
19
  VALIDATION_ERROR: "VALIDATION_ERROR",
20
20
  BAD_REQUEST: "BAD_REQUEST",
21
- NOT_FOUND: "NOT_FOUND"};
21
+ NOT_FOUND: "NOT_FOUND",
22
+ INTERNAL_ERROR: "INTERNAL_ERROR"
23
+ };
22
24
  var _KadoaSdkException = class _KadoaSdkException extends Error {
23
25
  constructor(message, options) {
24
26
  super(message);
@@ -100,7 +102,10 @@ _KadoaSdkException.ERROR_MESSAGES = {
100
102
  // Schema specific errors
101
103
  SCHEMA_NOT_FOUND: "Schema not found",
102
104
  SCHEMA_FETCH_ERROR: "Failed to fetch schema",
103
- SCHEMAS_FETCH_ERROR: "Failed to fetch schemas"
105
+ SCHEMAS_FETCH_ERROR: "Failed to fetch schemas",
106
+ SCHEMA_CREATE_FAILED: "Failed to create schema",
107
+ SCHEMA_UPDATE_FAILED: "Failed to update schema",
108
+ SCHEMA_DELETE_FAILED: "Failed to delete schema"
104
109
  };
105
110
  var KadoaSdkException = _KadoaSdkException;
106
111
  var ERROR_MESSAGES = KadoaSdkException.ERROR_MESSAGES;
@@ -4046,7 +4051,7 @@ var Configuration = class {
4046
4051
  };
4047
4052
 
4048
4053
  // src/version.ts
4049
- var SDK_VERSION = "0.12.0";
4054
+ var SDK_VERSION = "0.13.0";
4050
4055
  var SDK_NAME = "kadoa-node-sdk";
4051
4056
  var SDK_LANGUAGE = "node";
4052
4057
 
@@ -4482,12 +4487,197 @@ var NotificationsModule = class {
4482
4487
  return this.settingsService;
4483
4488
  }
4484
4489
  };
4490
+ var _SchemaBuilder = class _SchemaBuilder {
4491
+ constructor() {
4492
+ this.fields = [];
4493
+ }
4494
+ entity(entityName) {
4495
+ this.entityName = entityName;
4496
+ return this;
4497
+ }
4498
+ /**
4499
+ * Add a structured field to the schema
4500
+ * @param name - Field name (alphanumeric only)
4501
+ * @param description - Field description
4502
+ * @param dataType - Data type (STRING, NUMBER, BOOLEAN, etc.)
4503
+ * @param options - Optional field configuration
4504
+ */
4505
+ field(name, description, dataType, options) {
4506
+ this.validateFieldName(name);
4507
+ const requiresExample = _SchemaBuilder.TYPES_REQUIRING_EXAMPLE.includes(dataType);
4508
+ if (requiresExample && !options?.example) {
4509
+ throw new KadoaSdkException(
4510
+ `Field "${name}" with type ${dataType} requires an example`,
4511
+ { code: "VALIDATION_ERROR", details: { name, dataType } }
4512
+ );
4513
+ }
4514
+ this.fields.push({
4515
+ name,
4516
+ description,
4517
+ dataType,
4518
+ fieldType: "SCHEMA",
4519
+ example: options?.example,
4520
+ isKey: options?.isKey
4521
+ });
4522
+ return this;
4523
+ }
4524
+ /**
4525
+ * Add a classification field to categorize content
4526
+ * @param name - Field name (alphanumeric only)
4527
+ * @param description - Field description
4528
+ * @param categories - Array of category definitions
4529
+ */
4530
+ classify(name, description, categories) {
4531
+ this.validateFieldName(name);
4532
+ this.fields.push({
4533
+ name,
4534
+ description,
4535
+ fieldType: "CLASSIFICATION",
4536
+ categories
4537
+ });
4538
+ return this;
4539
+ }
4540
+ /**
4541
+ * Add raw page content to extract
4542
+ * @param name - Raw content format(s): "html", "markdown", or "url"
4543
+ */
4544
+ raw(name) {
4545
+ const names = Array.isArray(name) ? name : [name];
4546
+ for (const name2 of names) {
4547
+ const fieldName = `raw${upperFirst(camelCase(name2))}`;
4548
+ if (this.fields.some((field) => field.name === fieldName)) {
4549
+ continue;
4550
+ }
4551
+ this.fields.push({
4552
+ name: fieldName,
4553
+ description: `Raw page content in ${name2.toUpperCase()} format`,
4554
+ fieldType: "METADATA",
4555
+ metadataKey: name2
4556
+ });
4557
+ }
4558
+ return this;
4559
+ }
4560
+ build() {
4561
+ if (!this.entityName) {
4562
+ throw new KadoaSdkException("Entity name is required", {
4563
+ code: "VALIDATION_ERROR",
4564
+ details: { entityName: this.entityName }
4565
+ });
4566
+ }
4567
+ return {
4568
+ entityName: this.entityName,
4569
+ fields: this.fields
4570
+ };
4571
+ }
4572
+ validateFieldName(name) {
4573
+ if (!_SchemaBuilder.FIELD_NAME_PATTERN.test(name)) {
4574
+ throw new KadoaSdkException(
4575
+ `Field name "${name}" must be alphanumeric only (no underscores or special characters)`,
4576
+ {
4577
+ code: "VALIDATION_ERROR",
4578
+ details: { name, pattern: "^[A-Za-z0-9]+$" }
4579
+ }
4580
+ );
4581
+ }
4582
+ const lowerName = name.toLowerCase();
4583
+ if (this.fields.some((f) => f.name.toLowerCase() === lowerName)) {
4584
+ throw new KadoaSdkException(`Duplicate field name: "${name}"`, {
4585
+ code: "VALIDATION_ERROR",
4586
+ details: { name }
4587
+ });
4588
+ }
4589
+ }
4590
+ };
4591
+ _SchemaBuilder.FIELD_NAME_PATTERN = /^[A-Za-z0-9]+$/;
4592
+ _SchemaBuilder.TYPES_REQUIRING_EXAMPLE = [
4593
+ "STRING",
4594
+ "IMAGE",
4595
+ "LINK",
4596
+ "OBJECT",
4597
+ "ARRAY"
4598
+ ];
4599
+ var SchemaBuilder = _SchemaBuilder;
4485
4600
 
4486
4601
  // src/modules/schemas.module.ts
4602
+ var SchemaBuilderWithCreate = class extends SchemaBuilder {
4603
+ constructor(entityName, service) {
4604
+ super();
4605
+ this.service = service;
4606
+ this.entity(entityName);
4607
+ }
4608
+ /**
4609
+ * Create the schema directly in Kadoa
4610
+ * @param name - Optional schema name (defaults to entity name)
4611
+ * @returns Promise resolving to the created schema
4612
+ */
4613
+ async create(name) {
4614
+ const built = this.build();
4615
+ return this.service.createSchema({
4616
+ name: name || built.entityName,
4617
+ entity: built.entityName,
4618
+ fields: built.fields
4619
+ });
4620
+ }
4621
+ };
4487
4622
  var SchemasModule = class {
4488
4623
  constructor(service) {
4489
4624
  this.service = service;
4490
4625
  }
4626
+ /**
4627
+ * Create a new schema builder for fluent schema definition
4628
+ * @param entityName - The name of the entity this schema represents
4629
+ * @returns A new SchemaBuilder instance with the entity name already set
4630
+ * @example Build then create
4631
+ * ```typescript
4632
+ * const schema = kadoa.schema.builder("Product")
4633
+ * .field("title", "Product name", "STRING", { example: "iPhone 15" })
4634
+ * .field("price", "Product price", "NUMBER")
4635
+ * .build();
4636
+ *
4637
+ * await kadoa.schema.create(schema);
4638
+ * ```
4639
+ *
4640
+ * @example Fluent chain with create
4641
+ * ```typescript
4642
+ * const schema = await kadoa.schema.builder("Product")
4643
+ * .field("title", "Product name", "STRING", { example: "iPhone 15" })
4644
+ * .field("price", "Product price", "NUMBER")
4645
+ * .create("Product Schema");
4646
+ * ```
4647
+ */
4648
+ builder(entityName) {
4649
+ return new SchemaBuilderWithCreate(entityName, this.service);
4650
+ }
4651
+ /**
4652
+ * Get a schema by ID
4653
+ */
4654
+ async get(schemaId) {
4655
+ return this.service.getSchema(schemaId);
4656
+ }
4657
+ /**
4658
+ * List all schemas
4659
+ */
4660
+ async list() {
4661
+ return this.service.listSchemas();
4662
+ }
4663
+ /**
4664
+ * Create a new schema from a body
4665
+ */
4666
+ async create(body) {
4667
+ return this.service.createSchema(body);
4668
+ }
4669
+ /**
4670
+ * Update an existing schema
4671
+ */
4672
+ async update(schemaId, body) {
4673
+ return this.service.updateSchema(schemaId, body);
4674
+ }
4675
+ /**
4676
+ * Delete a schema
4677
+ */
4678
+ async delete(schemaId) {
4679
+ return this.service.deleteSchema(schemaId);
4680
+ }
4491
4681
  };
4492
4682
 
4493
4683
  // src/modules/user.module.ts
@@ -5304,7 +5494,6 @@ var SchemasService = class {
5304
5494
  }
5305
5495
  /**
5306
5496
  * Get a schema by ID
5307
- * //todo: use proper schema type for response from generated client (when avaialble)
5308
5497
  */
5309
5498
  async getSchema(schemaId) {
5310
5499
  debug4("Fetching schema with ID: %s", schemaId);
@@ -5323,6 +5512,49 @@ var SchemasService = class {
5323
5512
  }
5324
5513
  return schemaData;
5325
5514
  }
5515
+ /**
5516
+ * List all schemas
5517
+ */
5518
+ async listSchemas() {
5519
+ const response = await this.schemasApi.v4SchemasGet();
5520
+ return response.data.data;
5521
+ }
5522
+ /**
5523
+ * Create a new schema
5524
+ */
5525
+ async createSchema(body) {
5526
+ debug4("Creating schema with name: %s", body.name);
5527
+ const response = await this.schemasApi.v4SchemasPost({
5528
+ createSchemaBody: body
5529
+ });
5530
+ const schemaId = response.data.schemaId;
5531
+ if (!schemaId) {
5532
+ throw new KadoaSdkException(ERROR_MESSAGES.SCHEMA_CREATE_FAILED, {
5533
+ code: KadoaErrorCode.INTERNAL_ERROR
5534
+ });
5535
+ }
5536
+ return this.getSchema(schemaId);
5537
+ }
5538
+ /**
5539
+ * Update an existing schema
5540
+ */
5541
+ async updateSchema(schemaId, body) {
5542
+ debug4("Updating schema with ID: %s", schemaId);
5543
+ await this.schemasApi.v4SchemasSchemaIdPut({
5544
+ schemaId,
5545
+ updateSchemaBody: body
5546
+ });
5547
+ return this.getSchema(schemaId);
5548
+ }
5549
+ /**
5550
+ * Delete a schema
5551
+ */
5552
+ async deleteSchema(schemaId) {
5553
+ debug4("Deleting schema with ID: %s", schemaId);
5554
+ await this.schemasApi.v4SchemasSchemaIdDelete({
5555
+ schemaId
5556
+ });
5557
+ }
5326
5558
  };
5327
5559
 
5328
5560
  // src/internal/domains/notifications/notification-setup.service.ts
@@ -5793,12 +6025,12 @@ var EntityResolverService = class {
5793
6025
  fields: entityPrediction.fields
5794
6026
  };
5795
6027
  } else if (entityConfig) {
5796
- if ("schemId" in entityConfig) {
6028
+ if ("schemaId" in entityConfig) {
5797
6029
  const schema = await this.schemasService.getSchema(
5798
- entityConfig.schemId
6030
+ entityConfig.schemaId
5799
6031
  );
5800
6032
  return {
5801
- entity: schema.entity,
6033
+ entity: schema.entity ?? "",
5802
6034
  fields: schema.schema
5803
6035
  };
5804
6036
  } else if ("name" in entityConfig && "fields" in entityConfig) {
@@ -5859,187 +6091,6 @@ var EntityResolverService = class {
5859
6091
  }
5860
6092
  }
5861
6093
  };
5862
-
5863
- // src/internal/domains/extraction/builders/extraction-builder.ts
5864
- var ExtractionBuilder = class {
5865
- constructor() {
5866
- this.fields = [];
5867
- }
5868
- /**
5869
- * Start defining a custom schema with fields
5870
- * @param name - The entity name (e.g., "Product", "Article")
5871
- */
5872
- schema(name) {
5873
- if (this.schemaId) {
5874
- throw new KadoaSdkException(
5875
- "Cannot use schema() after useSchema() - they are mutually exclusive",
5876
- { code: "VALIDATION_ERROR" }
5877
- );
5878
- }
5879
- this.schemaName = name;
5880
- return new SchemaBuilder(this);
5881
- }
5882
- /**
5883
- * Use an existing schema by ID
5884
- * @param schemaId - The schema ID to reference
5885
- */
5886
- useSchema(schemaId) {
5887
- if (this.schemaName || this.fields.length > 0) {
5888
- throw new KadoaSdkException(
5889
- "Cannot use useSchema() after schema() or field definitions - they are mutually exclusive",
5890
- { code: "VALIDATION_ERROR" }
5891
- );
5892
- }
5893
- this.schemaId = schemaId;
5894
- return this;
5895
- }
5896
- /**
5897
- * Extract raw page content without transformation
5898
- * @param format - Raw content format(s): "html", "markdown", or "url"
5899
- */
5900
- raw(format) {
5901
- const formats = Array.isArray(format) ? format : [format];
5902
- const nameMap = {
5903
- html: "rawHtml",
5904
- markdown: "rawMarkdown",
5905
- url: "rawUrl"
5906
- };
5907
- const metadataKeyMap = {
5908
- html: "HTML",
5909
- markdown: "MARKDOWN",
5910
- url: "PAGE_URL"
5911
- };
5912
- for (const f of formats) {
5913
- const fieldName = nameMap[f];
5914
- if (this.fields.some((field) => field.name === fieldName)) {
5915
- continue;
5916
- }
5917
- this.fields.push({
5918
- name: fieldName,
5919
- description: `Raw page content in ${f.toUpperCase()} format`,
5920
- fieldType: "METADATA",
5921
- metadataKey: metadataKeyMap[f]
5922
- });
5923
- }
5924
- return this;
5925
- }
5926
- /**
5927
- * Get the fields array (internal use)
5928
- */
5929
- getFields() {
5930
- return this.fields;
5931
- }
5932
- /**
5933
- * Get the schema name (internal use)
5934
- */
5935
- getSchemaName() {
5936
- return this.schemaName;
5937
- }
5938
- /**
5939
- * Get the schema ID (internal use)
5940
- */
5941
- getSchemaId() {
5942
- return this.schemaId;
5943
- }
5944
- };
5945
- var _SchemaBuilder = class _SchemaBuilder extends ExtractionBuilder {
5946
- constructor(parentBuilder) {
5947
- super();
5948
- this.fields = parentBuilder.getFields();
5949
- this.schemaName = parentBuilder.getSchemaName();
5950
- this.schemaId = parentBuilder.getSchemaId();
5951
- }
5952
- /**
5953
- * Add a structured field to the schema
5954
- * @param name - Field name (alphanumeric only)
5955
- * @param description - Field description
5956
- * @param dataType - Data type (STRING, NUMBER, BOOLEAN, etc.)
5957
- * @param options - Optional field configuration
5958
- */
5959
- field(name, description, dataType, options) {
5960
- if (!_SchemaBuilder.FIELD_NAME_PATTERN.test(name)) {
5961
- throw new KadoaSdkException(
5962
- `Field name "${name}" must be alphanumeric only (no underscores or special characters)`,
5963
- {
5964
- code: "VALIDATION_ERROR",
5965
- details: { name, pattern: "^[A-Za-z0-9]+$" }
5966
- }
5967
- );
5968
- }
5969
- const lowerName = name.toLowerCase();
5970
- if (this.fields.some((f) => f.name.toLowerCase() === lowerName)) {
5971
- throw new KadoaSdkException(`Duplicate field name: "${name}"`, {
5972
- code: "VALIDATION_ERROR",
5973
- details: { name }
5974
- });
5975
- }
5976
- const requiresExample = _SchemaBuilder.TYPES_REQUIRING_EXAMPLE.includes(dataType);
5977
- if (requiresExample && !options?.example) {
5978
- throw new KadoaSdkException(
5979
- `Field "${name}" with type ${dataType} requires an example`,
5980
- { code: "VALIDATION_ERROR", details: { name, dataType } }
5981
- );
5982
- }
5983
- this.fields.push({
5984
- name,
5985
- description,
5986
- dataType,
5987
- fieldType: "SCHEMA",
5988
- example: options?.example,
5989
- isKey: options?.isKey
5990
- });
5991
- return this;
5992
- }
5993
- /**
5994
- * Add a classification field to categorize content
5995
- * @param name - Field name (alphanumeric only)
5996
- * @param description - Field description
5997
- * @param categories - Array of category definitions
5998
- */
5999
- classify(name, description, categories) {
6000
- if (!_SchemaBuilder.FIELD_NAME_PATTERN.test(name)) {
6001
- throw new KadoaSdkException(
6002
- `Field name "${name}" must be alphanumeric only (no underscores or special characters)`,
6003
- {
6004
- code: "VALIDATION_ERROR",
6005
- details: { name, pattern: "^[A-Za-z0-9]+$" }
6006
- }
6007
- );
6008
- }
6009
- const lowerName = name.toLowerCase();
6010
- if (this.fields.some((f) => f.name.toLowerCase() === lowerName)) {
6011
- throw new KadoaSdkException(`Duplicate field name: "${name}"`, {
6012
- code: "VALIDATION_ERROR",
6013
- details: { name }
6014
- });
6015
- }
6016
- this.fields.push({
6017
- name,
6018
- description,
6019
- fieldType: "CLASSIFICATION",
6020
- categories
6021
- });
6022
- return this;
6023
- }
6024
- /**
6025
- * Add raw page content alongside structured fields
6026
- * @param format - Raw content format(s): "html", "markdown", or "url"
6027
- */
6028
- raw(format) {
6029
- return super.raw(format);
6030
- }
6031
- };
6032
- _SchemaBuilder.FIELD_NAME_PATTERN = /^[A-Za-z0-9]+$/;
6033
- _SchemaBuilder.TYPES_REQUIRING_EXAMPLE = [
6034
- "STRING",
6035
- "IMAGE",
6036
- "LINK",
6037
- "OBJECT",
6038
- "ARRAY"
6039
- ];
6040
- var SchemaBuilder = _SchemaBuilder;
6041
-
6042
- // src/internal/domains/extraction/services/extraction-builder.service.ts
6043
6094
  var debug7 = logger.extraction;
6044
6095
  var ExtractionBuilderService = class {
6045
6096
  constructor(workflowsCoreService, entityResolverService, dataFetcherService, notificationSetupService) {
@@ -6075,18 +6126,12 @@ var ExtractionBuilderService = class {
6075
6126
  }) {
6076
6127
  let entity = "ai-detection";
6077
6128
  if (extraction) {
6078
- const builder = extraction(new ExtractionBuilder());
6079
- const schemaId = builder.getSchemaId();
6080
- const schemaName = builder.getSchemaName();
6081
- const fields = builder.getFields();
6082
- if (schemaId) {
6083
- entity = { schemId: schemaId };
6084
- } else if (schemaName && fields.length > 0) {
6085
- entity = { name: schemaName, fields };
6086
- } else if (fields.length > 0 && !schemaName) {
6087
- entity = { name: "RawExtraction", fields };
6129
+ const result = extraction(new SchemaBuilder());
6130
+ if ("schemaId" in result) {
6131
+ entity = { schemaId: result.schemaId };
6088
6132
  } else {
6089
- entity = "ai-detection";
6133
+ const builtSchema = result.build();
6134
+ entity = { name: builtSchema.entityName, fields: builtSchema.fields };
6090
6135
  }
6091
6136
  }
6092
6137
  this._options = {
@@ -6130,7 +6175,7 @@ var ExtractionBuilderService = class {
6130
6175
  async create() {
6131
6176
  assert(this._options, "Options are not set");
6132
6177
  const { urls, name, description, navigationMode, entity } = this.options;
6133
- const resolvedEntity = typeof entity === "object" && "schemId" in entity ? void 0 : await this.entityResolverService.resolveEntity(entity, {
6178
+ const resolvedEntity = typeof entity === "object" && "schemaId" in entity ? void 0 : await this.entityResolverService.resolveEntity(entity, {
6134
6179
  link: urls[0],
6135
6180
  location: this._options.location,
6136
6181
  navigationMode
@@ -6147,7 +6192,7 @@ var ExtractionBuilderService = class {
6147
6192
  description,
6148
6193
  navigationMode,
6149
6194
  monitoring: this._monitoringOptions,
6150
- schemaId: typeof entity === "object" && "schemId" in entity ? entity.schemId : void 0,
6195
+ schemaId: typeof entity === "object" && "schemaId" in entity ? entity.schemaId : void 0,
6151
6196
  entity: resolvedEntity.entity,
6152
6197
  fields: resolvedEntity.fields,
6153
6198
  autoStart: false,
@@ -6453,6 +6498,6 @@ var KadoaClient = class {
6453
6498
  }
6454
6499
  };
6455
6500
 
6456
- export { ERROR_MESSAGES, KadoaClient, KadoaHttpException, KadoaSdkException, SchemasModule, ValidationModule, pollUntil };
6501
+ export { ERROR_MESSAGES, KadoaClient, KadoaHttpException, KadoaSdkException, SchemaBuilder, SchemasModule, ValidationModule, pollUntil };
6457
6502
  //# sourceMappingURL=index.mjs.map
6458
6503
  //# sourceMappingURL=index.mjs.map