@narrative.io/data-collaboration-sdk-ts 0.0.31 → 0.0.33

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,19 +1,32 @@
1
- export interface Attribute {
1
+ export type Attribute = PrimitiveAttribute | ArrayAttribute | ObjectAttribute;
2
+ interface BaseAttribute {
2
3
  id: number;
3
4
  name: string;
5
+ display_name?: string;
4
6
  description?: string;
5
- display_name: string;
6
7
  type: string;
7
- enum?: string[] | number[];
8
8
  validations?: string[];
9
+ }
10
+ interface PrimitiveAttribute extends BaseAttribute {
11
+ type: 'string' | 'boolean' | 'double' | 'long' | 'timestamptz';
12
+ enum?: string[] | number[] | boolean[];
13
+ is_join_key?: boolean;
14
+ }
15
+ type SubAttribute = Partial<Omit<Attribute, 'name' | 'id'>> & {
16
+ type: string;
17
+ enum?: string[] | number[] | boolean[];
9
18
  is_join_key?: boolean;
10
- properties?: Record<string, {
11
- type: string;
12
- description?: string;
13
- validations?: string[];
14
- enum?: string[];
15
- items?: unknown;
16
- is_join_key?: boolean;
17
- }>;
19
+ };
20
+ interface RefAttribute {
21
+ $ref: number;
22
+ }
23
+ interface ArrayAttribute extends BaseAttribute {
24
+ type: 'array';
25
+ items: SubAttribute;
26
+ }
27
+ interface ObjectAttribute extends BaseAttribute {
28
+ type: 'object';
29
+ properties: Record<string, SubAttribute | RefAttribute>;
18
30
  required?: string[];
19
31
  }
32
+ export {};
@@ -1,3 +1,4 @@
1
+ import { type MandeInstance } from 'mande';
1
2
  import { type Config } from './types';
2
3
  /**
3
4
  * BaseApi class provides basic functionality for making API requests.
@@ -6,12 +7,37 @@ export declare abstract class BaseApi {
6
7
  private readonly apiKey?;
7
8
  private readonly environment;
8
9
  private readonly base_api;
10
+ private readonly headers;
9
11
  /**
10
12
  * @param {Config} config - Configuration object.
11
13
  * @param {string} [config.apiKey] - API key.
12
14
  * @param {Environment} [config.environment='prod'] - Environment for the API (e.g. 'dev', 'prod').
13
15
  */
14
16
  constructor(config: Config);
17
+ /**
18
+ * Retrieves the headers associated with the API instance.
19
+ *
20
+ * @returns An object containing key-value pairs representing the headers.
21
+ */
22
+ getHeaders(): Record<string, string>;
23
+ /**
24
+ * Retrieves the base API instance created using Mande.
25
+ *
26
+ * @returns A MandeInstance object representing the base API instance.
27
+ */
28
+ getBaseApi(): MandeInstance;
29
+ /**
30
+ * Retrieves the API key associated with the API instance, if it exists.
31
+ *
32
+ * @returns The API key as a string if it is set, or undefined if not set.
33
+ */
34
+ getApiKey(): string | undefined;
35
+ /**
36
+ * Retrieves the environment associated with the API instance.
37
+ *
38
+ * @returns A string representing the environment (e.g., 'dev', 'prod').
39
+ */
40
+ getEnvironment(): string;
15
41
  /**
16
42
  * Makes a GET request to the given endpoint.
17
43
  *
@@ -52,12 +78,12 @@ export declare abstract class BaseApi {
52
78
  * @returns {string} - Base URL for the API.
53
79
  * @private
54
80
  */
55
- private getBaseUrl;
81
+ getBaseUrl(): string;
56
82
  /**
57
83
  * Returns the headers for the API request.
58
84
  *
59
85
  * @returns {Object.<string, string>} - Headers for the API request.
60
86
  * @private
61
87
  */
62
- private getHeaders;
88
+ private constructHeaders;
63
89
  }
package/build/base-api.js CHANGED
@@ -6,6 +6,7 @@ export class BaseApi {
6
6
  apiKey;
7
7
  environment;
8
8
  base_api;
9
+ headers;
9
10
  /**
10
11
  * @param {Config} config - Configuration object.
11
12
  * @param {string} [config.apiKey] - API key.
@@ -14,10 +15,43 @@ export class BaseApi {
14
15
  constructor(config) {
15
16
  this.apiKey = config.apiKey;
16
17
  this.environment = config.environment ?? 'prod';
18
+ this.headers = config.headers ?? {};
17
19
  this.base_api = mande(`${this.getBaseUrl()}`, {
18
- headers: this.getHeaders()
20
+ headers: this.constructHeaders()
19
21
  });
20
22
  }
23
+ /**
24
+ * Retrieves the headers associated with the API instance.
25
+ *
26
+ * @returns An object containing key-value pairs representing the headers.
27
+ */
28
+ getHeaders() {
29
+ return this.constructHeaders();
30
+ }
31
+ /**
32
+ * Retrieves the base API instance created using Mande.
33
+ *
34
+ * @returns A MandeInstance object representing the base API instance.
35
+ */
36
+ getBaseApi() {
37
+ return this.base_api;
38
+ }
39
+ /**
40
+ * Retrieves the API key associated with the API instance, if it exists.
41
+ *
42
+ * @returns The API key as a string if it is set, or undefined if not set.
43
+ */
44
+ getApiKey() {
45
+ return this.apiKey;
46
+ }
47
+ /**
48
+ * Retrieves the environment associated with the API instance.
49
+ *
50
+ * @returns A string representing the environment (e.g., 'dev', 'prod').
51
+ */
52
+ getEnvironment() {
53
+ return this.environment;
54
+ }
21
55
  /**
22
56
  * Makes a GET request to the given endpoint.
23
57
  *
@@ -85,11 +119,12 @@ export class BaseApi {
85
119
  * @returns {Object.<string, string>} - Headers for the API request.
86
120
  * @private
87
121
  */
88
- getHeaders() {
122
+ constructHeaders() {
89
123
  const headers = {
90
- 'Content-type': 'application/json'
124
+ ...this.headers,
125
+ 'Content-Type': this.headers['Content-Type'] ?? 'application/json'
91
126
  };
92
- if (this.apiKey != null) {
127
+ if (!('Authorization' in headers) && (this.apiKey != null)) {
93
128
  headers.Authorization = `Bearer ${this.apiKey}`;
94
129
  }
95
130
  return headers;
@@ -1,6 +1,7 @@
1
1
  import { type ApiRecords } from '../types';
2
2
  import { BaseApi } from '../base-api';
3
- import { type Dataset, type DatasetTableSummaryAPIResponse } from './types';
3
+ import { type FilePerSnapshotResponse, type Dataset, type DatasetTableSummaryAPIResponse } from './types';
4
+ export { type FilePerSnapshotResponse, type Dataset, type DatasetTableSummaryAPIResponse };
4
5
  /**
5
6
  * @module DatasetApi
6
7
  * @description This module provides methods for fetching datasets.
@@ -42,4 +43,32 @@ export declare class DatasetApi extends BaseApi {
42
43
  const records = await getDatasetSample(datasetId, size);
43
44
  */
44
45
  getDatasetSample(datasetId: number, size?: number): Promise<ApiRecords<Record<string, string>>>;
46
+ /**
47
+ * Fetches a list of dataset files based on the provided parameters.
48
+ *
49
+ * @param {number} datasetId - The ID of the dataset to fetch files for.
50
+ * @param {number} [size] - The number of snapshots to return. Optional.
51
+ * @param {number} [offset] - The starting index of the file list. Optional.
52
+ * @param {string} [afterTimestamp] - Fetch files with a timestamp greater than or equal to the given value. Optional.
53
+ * @param {number} [fromSnapshotId] - Fetch files from the specified snapshot ID. Optional.
54
+ * @returns {Promise<FilePerSnapshotResponse>} A Promise that resolves with the fetched dataset files.
55
+ * @throws {Error} If there is an issue with the request or the response.
56
+ * @memberof DatasetApi
57
+ * @public
58
+ */
59
+ getDatasetFiles(datasetId: number, size?: number, offset?: number, afterTimestamp?: string, fromSnapshotId?: number): Promise<FilePerSnapshotResponse>;
60
+ /**
61
+ * Fetches the download URL for a specific dataset file.
62
+ *
63
+ * @param {number} datasetId - The ID of the dataset to fetch the file download URL for.
64
+ * @param {number} snapshotId - The ID of the snapshot the file is associated with.
65
+ * @param {string} fileId - The ID of the file to fetch the download URL for.
66
+ * @returns {Promise<{ download_url: string }>} A Promise that resolves with an object containing the download URL.
67
+ * @throws {Error} If there is an issue with the request or the response.
68
+ * @memberof DatasetApi
69
+ * @public
70
+ */
71
+ getDatasetFileDownloadUrl(datasetId: number, snapshotId: number, fileId: string): Promise<{
72
+ download_url: string;
73
+ }>;
45
74
  }
@@ -49,4 +49,43 @@ export class DatasetApi extends BaseApi {
49
49
  async getDatasetSample(datasetId, size = 1000) {
50
50
  return await this.get(`${resourceName}/${datasetId}/sample?size=${size}`);
51
51
  }
52
+ /**
53
+ * Fetches a list of dataset files based on the provided parameters.
54
+ *
55
+ * @param {number} datasetId - The ID of the dataset to fetch files for.
56
+ * @param {number} [size] - The number of snapshots to return. Optional.
57
+ * @param {number} [offset] - The starting index of the file list. Optional.
58
+ * @param {string} [afterTimestamp] - Fetch files with a timestamp greater than or equal to the given value. Optional.
59
+ * @param {number} [fromSnapshotId] - Fetch files from the specified snapshot ID. Optional.
60
+ * @returns {Promise<FilePerSnapshotResponse>} A Promise that resolves with the fetched dataset files.
61
+ * @throws {Error} If there is an issue with the request or the response.
62
+ * @memberof DatasetApi
63
+ * @public
64
+ */
65
+ async getDatasetFiles(datasetId, size, offset, afterTimestamp, fromSnapshotId) {
66
+ const params = new URLSearchParams();
67
+ if (size != null)
68
+ params.append('per_page', String(size));
69
+ if (offset != null)
70
+ params.append('offset', String(offset));
71
+ if (afterTimestamp != null)
72
+ params.append('timestamp.gte', afterTimestamp);
73
+ if (fromSnapshotId != null)
74
+ params.append('from_snapshot_id', String(fromSnapshotId));
75
+ return await this.get(`${resourceName}/${datasetId}/find-files?${params.toString()}`);
76
+ }
77
+ /**
78
+ * Fetches the download URL for a specific dataset file.
79
+ *
80
+ * @param {number} datasetId - The ID of the dataset to fetch the file download URL for.
81
+ * @param {number} snapshotId - The ID of the snapshot the file is associated with.
82
+ * @param {string} fileId - The ID of the file to fetch the download URL for.
83
+ * @returns {Promise<{ download_url: string }>} A Promise that resolves with an object containing the download URL.
84
+ * @throws {Error} If there is an issue with the request or the response.
85
+ * @memberof DatasetApi
86
+ * @public
87
+ */
88
+ async getDatasetFileDownloadUrl(datasetId, snapshotId, fileId) {
89
+ return await this.get(`${resourceName}/${datasetId}/snapshots/${snapshotId}/files-added/${fileId}/download`);
90
+ }
52
91
  }
@@ -74,4 +74,18 @@ interface Schema {
74
74
  required: string[];
75
75
  primary: string;
76
76
  }
77
+ interface File {
78
+ path: string;
79
+ size: number;
80
+ }
81
+ interface Snapshot {
82
+ files: File[];
83
+ snapshot_id: number;
84
+ timestamp: string;
85
+ }
86
+ export interface FilePerSnapshotResponse {
87
+ files_per_snapshot: Snapshot[];
88
+ has_next: boolean;
89
+ next_snapshot: number | null;
90
+ }
77
91
  export {};
package/build/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import { DatasetApi } from './datasets';
3
3
  import { HealthCheckApi } from './health';
4
4
  import { RosettaStoneApi } from './rosetta-stone';
5
5
  import { type Environment, type Config, type ApiRecords, type MonetaryAmount } from './types';
6
- import { type DatasetTableSummaryAPIResponse, type Dataset, type DatasetTableSummary } from './datasets/types';
6
+ import { type DatasetTableSummaryAPIResponse, type Dataset, type DatasetTableSummary, type FilePerSnapshotResponse } from './datasets/types';
7
7
  import { type HealthCheckResult } from './health/types';
8
8
  import { type SampleRecords } from './rosetta-stone/types';
9
9
  import { AttributeApi } from './attributes';
@@ -19,4 +19,4 @@ declare class NarrativeApi extends BaseApi {
19
19
  }
20
20
  interface NarrativeApi extends BaseApi, HealthCheckApi, DatasetApi, RosettaStoneApi, AttributeApi, PingApi, CompanyInfoApi, InstallationsApi, UploadsApi {
21
21
  }
22
- export { NarrativeApi, AttributeApi, HealthCheckApi, DatasetApi, PingApi, ProductsApi, CompanyInfoApi, InstallationsApi, UploadsApi, type UploadsResponse, type PingStatus, type Attribute, type Environment, type Config, type ApiRecords, type Dataset, type HealthCheckResult, type SampleRecords, type DatasetTableSummary, type DatasetTableSummaryAPIResponse, type Product, type MonetaryAmount, type CompanyInfo, type Installation };
22
+ export { NarrativeApi, AttributeApi, HealthCheckApi, DatasetApi, PingApi, ProductsApi, CompanyInfoApi, InstallationsApi, UploadsApi, type UploadsResponse, type PingStatus, type Attribute, 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 };
@@ -0,0 +1,19 @@
1
+ import { type Config } from 'src/types';
2
+ import { BaseApi } from '../base-api';
3
+ import { type NqlResult } from './types';
4
+ /**
5
+ * A class for accessing the NQL API.
6
+ * @extends BaseApi
7
+ */
8
+ declare class NqlApi extends BaseApi {
9
+ constructor(config: Config);
10
+ /**
11
+ * Executes an NQL query and returns the result.
12
+ *
13
+ * @param {string} token - The bearer token for authorization.
14
+ * @param {string} nqlQuery - The NQL query to execute.
15
+ * @returns {Promise<object>} A promise that resolves with the result of the NQL query.
16
+ */
17
+ executeNql(token: string, nqlQuery: string): Promise<object>;
18
+ }
19
+ export { NqlApi, type NqlResult };
@@ -0,0 +1,35 @@
1
+ import { BaseApi } from '../base-api';
2
+ /**
3
+ * The name of the resource to be used in API requests.
4
+ * @constant
5
+ * @private
6
+ * @type {string}
7
+ */
8
+ const resourceName = 'nql';
9
+ /**
10
+ * A class for accessing the NQL API.
11
+ * @extends BaseApi
12
+ */
13
+ class NqlApi extends BaseApi {
14
+ constructor(config) {
15
+ super({
16
+ ...config,
17
+ headers: {
18
+ ...config.headers,
19
+ 'Content-Type': 'application/vnd.nql'
20
+ }
21
+ });
22
+ }
23
+ /**
24
+ * Executes an NQL query and returns the result.
25
+ *
26
+ * @param {string} token - The bearer token for authorization.
27
+ * @param {string} nqlQuery - The NQL query to execute.
28
+ * @returns {Promise<object>} A promise that resolves with the result of the NQL query.
29
+ */
30
+ async executeNql(token, nqlQuery) {
31
+ const response = await this.post(resourceName, nqlQuery);
32
+ return response;
33
+ }
34
+ }
35
+ export { NqlApi };
@@ -0,0 +1,28 @@
1
+ export interface NqlResult {
2
+ id: string;
3
+ company_id: number;
4
+ completed_at: string;
5
+ created_at: string;
6
+ failures: any[];
7
+ idempotency_key: string;
8
+ input: {
9
+ nql: string;
10
+ };
11
+ request_source: {
12
+ type: string;
13
+ company_id: number;
14
+ user_id: number;
15
+ };
16
+ result: {
17
+ success: {
18
+ result: {
19
+ forecast: {
20
+ rows: number;
21
+ cost: number;
22
+ };
23
+ };
24
+ };
25
+ };
26
+ state: string;
27
+ updated_at: string;
28
+ }
@@ -0,0 +1 @@
1
+ export {};
package/build/types.d.ts CHANGED
@@ -14,6 +14,7 @@ export type Environment = 'dev' | 'prod' | string;
14
14
  export interface Config {
15
15
  apiKey?: string;
16
16
  environment?: Environment;
17
+ headers?: Record<string, string>;
17
18
  }
18
19
  export interface ApiRecords<T> {
19
20
  records: T[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narrative.io/data-collaboration-sdk-ts",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "main": "build/index.js",
5
5
  "repository": "github:narrative-io/data-collaboration-sdk-ts",
6
6
  "source": "src/index.ts",
@@ -9,13 +9,12 @@
9
9
  "scripts": {
10
10
  "build": "tsc",
11
11
  "lint": "npx eslint . --fix",
12
- "prepare": "npx eslint . --fix && npm run build",
12
+ "prepare": "npx eslint . --fix && npm run build && husky install",
13
13
  "prepublishOnly": "npm test && npm run lint",
14
14
  "preversion": "npm run lint",
15
15
  "version": "npm run && git add -A src",
16
16
  "postversion": "git push && git push --tags",
17
- "test": "jest",
18
- "coverage": "jest --coverage"
17
+ "test": "jest --coverage"
19
18
  },
20
19
  "keywords": [],
21
20
  "author": "",
@@ -36,7 +35,8 @@
36
35
  "eslint-plugin-promise": "^6.1.1",
37
36
  "jest": "^29.4.3",
38
37
  "ts-jest": "^29.0.5",
39
- "typescript": "^4.9.5"
38
+ "typescript": "^5.0.2",
39
+ "husky": "^8.0.0"
40
40
  },
41
41
  "dependencies": {
42
42
  "mande": "^2.0.3"