@narrative.io/data-collaboration-sdk-ts 0.0.74 → 0.0.76

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.
@@ -1,5 +1,5 @@
1
1
  import { BaseApi } from '../base-api';
2
- import { type LoginRequest, type LoginResponse, type RegisterRequest, type RegisterResponse, type LoggedInUser, type StytchToken } from './types';
2
+ import { type LoginRequest, type LoginResponse, type RegisterRequest, type RegisterResponse, type LoggedInUser, type StytchToken, type ApiToken, type UserRole } from './types';
3
3
  /**
4
4
  * A class for accessing the Authentication API
5
5
  * @extends BaseApi
@@ -20,4 +20,4 @@ declare class AuthenticationApi extends BaseApi {
20
20
  */
21
21
  register(registerRequest: RegisterRequest): Promise<RegisterResponse>;
22
22
  }
23
- export { type LoginRequest, type LoginResponse, type RegisterRequest, type RegisterResponse, type LoggedInUser, type StytchToken, AuthenticationApi };
23
+ export { type LoginRequest, type LoginResponse, type RegisterRequest, type RegisterResponse, type LoggedInUser, type StytchToken, type ApiToken, type UserRole, AuthenticationApi };
@@ -3,11 +3,25 @@ export interface LoginRequest {
3
3
  session_duration_minutes?: number;
4
4
  }
5
5
  export interface LoginResponse {
6
- user: LoggedInUser;
7
- api_token: string;
6
+ user: {
7
+ id: number;
8
+ email: string;
9
+ name: string;
10
+ company_id: number;
11
+ company_name: string;
12
+ role: number;
13
+ };
14
+ api_token: ApiToken;
15
+ legacy_token: string;
8
16
  stytch_session_token: string;
9
17
  stytch_session_jwt: string;
10
18
  }
19
+ export interface ApiToken {
20
+ access_token: string;
21
+ access_token_expires_in: number;
22
+ refresh_token: string;
23
+ refresh_token_expires_in: number;
24
+ }
11
25
  export interface RegisterRequest {
12
26
  stytch_token: StytchToken;
13
27
  name?: string;
@@ -18,11 +32,14 @@ export interface RegisterResponse {
18
32
  }
19
33
  export interface LoggedInUser {
20
34
  id: number;
35
+ email?: string;
21
36
  name: string;
22
37
  company_id: number;
23
38
  company_name: string;
39
+ role: UserRole;
24
40
  }
25
41
  export interface StytchToken {
26
42
  token: string;
27
43
  token_type: 'magiclinks' | 'oauth';
28
44
  }
45
+ export type UserRole = 'global_admin' | 'partner_admin' | 'partner_user';
package/build/index.d.ts CHANGED
@@ -22,11 +22,11 @@ import { ForecastApi, type ForecastRequest, type ForecastResponse, type CostFore
22
22
  import { ContractsApi, type ContractDetails, type CustomerContract, type PaymentMethod, type ContractRateView } from './contracts';
23
23
  import { getNqlObject, buildNql } from './nql/NQLParser';
24
24
  import { convertNqlObjectToForecastBody } from './nql/DataRulesConverter';
25
- import { AuthenticationApi, type LoginRequest, type LoginResponse, type RegisterRequest, type RegisterResponse, type LoggedInUser, type StytchToken } from './authentication';
25
+ import { AuthenticationApi, type LoginRequest, type LoginResponse, type RegisterRequest, type RegisterResponse, type LoggedInUser, type StytchToken, type ApiToken, type UserRole } from './authentication';
26
26
  import { AppsApi, type App } from './apps';
27
27
  import type { ForecastBody } from './nql/types';
28
28
  declare class NarrativeApi extends BaseApi {
29
29
  }
30
30
  interface NarrativeApi extends BaseApi, HealthCheckApi, DatasetApi, RosettaStoneApi, AttributeApi, PingApi, CompanyInfoApi, InstallationsApi, UploadsApi, ResourceApi, NqlApi, DataStreamsApi, ForecastApi, ContractsApi, AuthenticationApi {
31
31
  }
32
- export { NarrativeApi, AttributeApi, HealthCheckApi, DatasetApi, PingApi, ProductsApi, CompanyInfoApi, InstallationsApi, UploadsApi, ResourceApi, NqlApi, DataStreamsApi, ForecastApi, ContractsApi, AuthenticationApi, AppsApi, type App, type Resource, type UploadsResponse, type PingStatus, type Attribute, type RefAttribute, type SubAttribute, type Environment, type Config, type ApiRecords, type Dataset, type HealthCheckResult, type SampleRecords, type DatasetTableSummary, type DatasetTableSummaryAPIResponse, type Product, type MonetaryAmount, type CompanyInfo, type Installation, type FilePerSnapshotResponse, type BucketCreationRequest, type UpdateAccessTypeRequest, type Nql, type NqlField, type NqlResult, type NqlQueryInput, type NqlCompileResult, type NqlExpression, type NqlWhere, type NqlFilterExpression, type NqlBooleanExpression, type NqlFilterBinaryExpression, type NqlFilterUnaryExpression, type DataStream, type ForecastRequest, type ForecastResponse, type CostForecastRequest, type CostForecastResponse, type JobState, type ContractDetails, type CustomerContract, type PaymentMethod, type ContractRateView, type LoginRequest, type LoginResponse, type RegisterRequest, type RegisterResponse, type LoggedInUser, type StytchToken, getNqlObject, buildNql, convertNqlObjectToForecastBody, type ForecastBody };
32
+ export { NarrativeApi, AttributeApi, HealthCheckApi, DatasetApi, PingApi, ProductsApi, CompanyInfoApi, InstallationsApi, UploadsApi, ResourceApi, NqlApi, DataStreamsApi, ForecastApi, ContractsApi, AuthenticationApi, AppsApi, type App, type Resource, type UploadsResponse, type PingStatus, type Attribute, type RefAttribute, type SubAttribute, type Environment, type Config, type ApiRecords, type Dataset, type HealthCheckResult, type SampleRecords, type DatasetTableSummary, type DatasetTableSummaryAPIResponse, type Product, type MonetaryAmount, type CompanyInfo, type Installation, type FilePerSnapshotResponse, type BucketCreationRequest, type UpdateAccessTypeRequest, type Nql, type NqlField, type NqlResult, type NqlQueryInput, type NqlCompileResult, type NqlExpression, type NqlWhere, type NqlFilterExpression, type NqlBooleanExpression, type NqlFilterBinaryExpression, type NqlFilterUnaryExpression, type DataStream, type ForecastRequest, type ForecastResponse, type CostForecastRequest, type CostForecastResponse, type JobState, type ContractDetails, type CustomerContract, type PaymentMethod, type ContractRateView, type LoginRequest, type LoginResponse, type RegisterRequest, type RegisterResponse, type LoggedInUser, type StytchToken, type ApiToken, type UserRole, getNqlObject, buildNql, convertNqlObjectToForecastBody, type ForecastBody };
@@ -1,4 +1,6 @@
1
- import type { ForecastBody, Nql } from './types';
1
+ import { type ObjectAttribute } from 'src/attributes/types';
2
+ import { type Attribute } from '../attributes';
3
+ import type { DataRulesAttributesField, ForecastBody, Nql } from './types';
2
4
  /**
3
5
  * Convert an NQL object to a Forecast body.
4
6
  * @param {Nql} nqlObject - The NQL object to convert.
@@ -6,3 +8,43 @@ import type { ForecastBody, Nql } from './types';
6
8
  * @throws {Error} - Throws an error if price filter conditions are not met.
7
9
  */
8
10
  export declare function convertNqlObjectToForecastBody(nqlObject: Nql): Promise<ForecastBody>;
11
+ /**
12
+ * Looks for an attribute with the given name and checks if it is of type 'object'.
13
+ *
14
+ * @param {Attribute[]} attributes - An array of attributes to search through.
15
+ * @param {string} name - The name of the attribute to look for.
16
+ * @returns {ObjectAttribute | undefined} Returns the attribute if found and is of type 'object', otherwise returns undefined.
17
+ *
18
+ * @example
19
+ * // Assuming attributes is an array of Attribute objects with properties 'name', 'type', and optionally 'properties'.
20
+ * const attributes = [{ name: 'person', type: 'object', properties: { name: 'John Doe' } }];
21
+ * // Returns { name: 'person', type: 'object', properties: { name: 'John Doe' } }
22
+ * isObject(attributes, 'person');
23
+ */
24
+ export declare function isObject(attributes: Attribute[], name: string): ObjectAttribute | undefined;
25
+ /**
26
+ * Finds an attribute with the given name, and if it is of type 'object', returns the names of its properties.
27
+ *
28
+ * @param {Attribute[]} attributes - An array of attributes to search through.
29
+ * @param {string} name - The name of the attribute to look for.
30
+ * @returns {string[]} An array containing the names of the properties if the attribute is of type 'object', otherwise an empty array.
31
+ *
32
+ * @example
33
+ * // Assuming attributes is an array of Attribute objects with properties 'name', 'type', and optionally 'properties'.
34
+ * const attributes = [{ name: 'person', type: 'object', properties: { name: 'John Doe' } }];
35
+ * // Returns ['name']
36
+ * findAttributeProperties(attributes, 'person');
37
+ */
38
+ export declare function findAttributeProperties(attributes: Attribute[], name: string): string[];
39
+ /**
40
+ * Builds an array of field objects with names based on the given attribute and properties.
41
+ *
42
+ * @param {string} attribute - The primary attribute to be prefixed to each field.
43
+ * @param {string[]} properties - An array of properties that will be appended to the attribute to form the field names.
44
+ * @returns {DataRulesAttributesField[]} An array of field objects, where each object has a 'field' property which is a combination of the attribute and property, and an 'exported' property set to true.
45
+ *
46
+ * @example
47
+ * // Returns [{ field: 'person.name', exported: true }, { field: 'person.age', exported: true }]
48
+ * buildField('person', ['name', 'age']);
49
+ */
50
+ export declare function buildField(attribute: string, properties: string[]): DataRulesAttributesField[];
@@ -34,13 +34,15 @@ async function getAttributesFromFields(fields) {
34
34
  const attributes = await attributesApi.getAttributes();
35
35
  const uniqueAttributes = [...new Set(fields.map((field) => field.field.split('.')[0]))].filter((att) => !att.startsWith('_'));
36
36
  uniqueAttributes.forEach((att) => {
37
+ const isObjectProperty = isObject(attributes.records, att);
38
+ const relevantFields = (isObjectProperty != null) ? fields.filter((field) => field.field.split('.')[0] === att && field.field !== att) : fields.filter((field) => field.field.split('.')[0] === att);
37
39
  const attributeId = attributes.records.find((attribute) => attribute.name === att)?.id;
38
40
  if (attributeId === undefined) {
39
41
  throw new Error(`Attribute ${att} not found`);
40
42
  }
41
43
  attributesArray.push({
42
44
  attribute_id: attributeId,
43
- fields: fields.filter((field) => field.field.split('.')[0] === att),
45
+ fields: relevantFields.length > 0 ? relevantFields : buildField(att, findAttributeProperties(attributes.records, att)),
44
46
  optional: fields.some(field => field.field.startsWith(att + '.') && field.filter === undefined)
45
47
  });
46
48
  });
@@ -212,3 +214,65 @@ function addParentheses(input) {
212
214
  // Replace matched string with itself followed by ()
213
215
  return input.replace(regex, '$1()');
214
216
  }
217
+ /**
218
+ * Looks for an attribute with the given name and checks if it is of type 'object'.
219
+ *
220
+ * @param {Attribute[]} attributes - An array of attributes to search through.
221
+ * @param {string} name - The name of the attribute to look for.
222
+ * @returns {ObjectAttribute | undefined} Returns the attribute if found and is of type 'object', otherwise returns undefined.
223
+ *
224
+ * @example
225
+ * // Assuming attributes is an array of Attribute objects with properties 'name', 'type', and optionally 'properties'.
226
+ * const attributes = [{ name: 'person', type: 'object', properties: { name: 'John Doe' } }];
227
+ * // Returns { name: 'person', type: 'object', properties: { name: 'John Doe' } }
228
+ * isObject(attributes, 'person');
229
+ */
230
+ export function isObject(attributes, name) {
231
+ // Look for the attribute with the given name.
232
+ const attribute = attributes.find(attr => attr.name === name);
233
+ // If no such attribute is found, return an empty array.
234
+ if (attribute == null) {
235
+ return undefined;
236
+ }
237
+ return attribute.type === 'object' && 'properties' in attribute ? attribute : undefined;
238
+ }
239
+ /**
240
+ * Finds an attribute with the given name, and if it is of type 'object', returns the names of its properties.
241
+ *
242
+ * @param {Attribute[]} attributes - An array of attributes to search through.
243
+ * @param {string} name - The name of the attribute to look for.
244
+ * @returns {string[]} An array containing the names of the properties if the attribute is of type 'object', otherwise an empty array.
245
+ *
246
+ * @example
247
+ * // Assuming attributes is an array of Attribute objects with properties 'name', 'type', and optionally 'properties'.
248
+ * const attributes = [{ name: 'person', type: 'object', properties: { name: 'John Doe' } }];
249
+ * // Returns ['name']
250
+ * findAttributeProperties(attributes, 'person');
251
+ */
252
+ export function findAttributeProperties(attributes, name) {
253
+ const attribute = isObject(attributes, name);
254
+ // If the attribute is of type 'object', return the names of its properties.
255
+ if (attribute != null) {
256
+ return Object.keys(attribute.properties);
257
+ }
258
+ return [];
259
+ }
260
+ /**
261
+ * Builds an array of field objects with names based on the given attribute and properties.
262
+ *
263
+ * @param {string} attribute - The primary attribute to be prefixed to each field.
264
+ * @param {string[]} properties - An array of properties that will be appended to the attribute to form the field names.
265
+ * @returns {DataRulesAttributesField[]} An array of field objects, where each object has a 'field' property which is a combination of the attribute and property, and an 'exported' property set to true.
266
+ *
267
+ * @example
268
+ * // Returns [{ field: 'person.name', exported: true }, { field: 'person.age', exported: true }]
269
+ * buildField('person', ['name', 'age']);
270
+ */
271
+ export function buildField(attribute, properties) {
272
+ return properties.map((property) => {
273
+ return {
274
+ field: `${attribute}.${property}`,
275
+ exported: true
276
+ };
277
+ });
278
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narrative.io/data-collaboration-sdk-ts",
3
- "version": "0.0.74",
3
+ "version": "0.0.76",
4
4
  "main": "build/index.js",
5
5
  "repository": "github:narrative-io/data-collaboration-sdk-ts",
6
6
  "source": "src/index.ts",