Skip to content

Commit

Permalink
feat: add notes (VF-3209) (#228)
Browse files Browse the repository at this point in the history
<!-- You can erase any parts of this template not applicable to your Pull Request. -->

**Fixes or implements VF-000**

### Checklist

- [ ] this is a breaking change and should publish a new major version
- [ ] appropriate tests have been written
  • Loading branch information
z4o4z committed Mar 25, 2022
1 parent f8721bd commit 29c63ec
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/api-sdk/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientOptions, PublicClient } from '@api-sdk/publicclient';
import { User } from '@api-sdk/resources';
import { ClientOptions, PublicClient } from './publicClient';
import { User } from './resources';

export class Client extends PublicClient {
public user: User;
Expand Down
11 changes: 6 additions & 5 deletions packages/api-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Client } from '@api-sdk/client';
import { ClientOptions, PublicClient } from '@api-sdk/publicclient';
import * as s from 'superstruct';

export type { Client } from '@api-sdk/client';
export type { PublicClient } from '@api-sdk/publicclient';
export type { Flatten } from '@api-sdk/types';
import { Client } from './client';
import { ClientOptions, PublicClient } from './publicClient';

export type { Client } from './client';
export type { PublicClient } from './publicClient';
export type { Flatten } from './types';

export const SParams = s.object({
clientKey: s.string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Fetch, { FetchConfig } from '@api-sdk/fetch';
import { Analytics, APIKey, Diagram, Program, Project, PrototypeProgram, VariableState, Version } from '@api-sdk/resources';
import { Crypto } from '@voiceflow/common';

import Fetch, { FetchConfig } from './fetch';
import { Analytics, APIKey, Diagram, Note, Program, Project, PrototypeProgram, VariableState, Version } from './resources';

export interface ClientOptions {
options?: FetchConfig;
clientKey: string;
Expand All @@ -11,8 +12,12 @@ export interface ClientOptions {
}

export class PublicClient {
public note: Note;

public fetch: Fetch;

public apiKey: APIKey;

public project: Project;

public version: Version;
Expand All @@ -23,22 +28,21 @@ export class PublicClient {

public analytics: Analytics;

public apiKey: APIKey;
public variableState: VariableState;

public prototypeProgram: PrototypeProgram;

public variableState: VariableState;

constructor({ clientKey, apiEndpoint, authorization, options, analyticsEncryption }: ClientOptions) {
this.fetch = new Fetch({ clientKey, apiEndpoint, authorization, options });

this.note = new Note(this.fetch);
this.apiKey = new APIKey(this.fetch);
this.project = new Project(this.fetch);
this.version = new Version(this.fetch);
this.program = new Program(this.fetch);
this.diagram = new Diagram(this.fetch);
this.analytics = new Analytics(this.fetch, { encryption: analyticsEncryption });
this.prototypeProgram = new PrototypeProgram(this.fetch);
this.variableState = new VariableState(this.fetch);
this.prototypeProgram = new PrototypeProgram(this.fetch);
}
}
6 changes: 3 additions & 3 deletions packages/api-sdk/src/resources/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Fetcher, { FetcherOptions } from './fetcher';

export type Fields = string[] | ReadonlyArray<string>;

export type BaseResourceOptions<C extends Record<string, any>> = FetcherOptions<C>;
export type BaseResourceOptions<Client extends Record<string, any>> = FetcherOptions<Client>;

class BaseResource<C extends Record<string, any>> extends Fetcher<C> {
constructor(options: BaseResourceOptions<C>) {
class BaseResource<Client extends Record<string, any>> extends Fetcher<Client> {
constructor(options: BaseResourceOptions<Client>) {
super(options);
}

Expand Down
1 change: 1 addition & 0 deletions packages/api-sdk/src/resources/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { default as Analytics } from './analytics';
export { default as APIKey } from './apiKey';
export { default as Diagram } from './diagram';
export { default as Note } from './note';
export { default as Program } from './program';
export { default as Project } from './project';
export { default as PrototypeProgram } from './prototypeProgram';
Expand Down
26 changes: 26 additions & 0 deletions packages/api-sdk/src/resources/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Fetch from '@api-sdk/fetch';
import { BaseModels } from '@voiceflow/base-types';

import BaseResource from './base';

export const ENDPOINT = 'notes';

export type ModelKey = 'id';

class NoteResource extends BaseResource<NoteResource> {
constructor(fetch: Fetch) {
super({ fetch, clazz: NoteResource, endpoint: ENDPOINT });
}

public async upsert<T extends BaseModels.BaseNote = BaseModels.BaseNote>(versionID: string, note: T): Promise<T> {
const { data } = await this.fetch.put<T>(`${this._getEndpoint()}/${versionID}`, { note });

return data;
}

public async delete(versionID: string, noteID: string): Promise<void> {
await this.fetch.delete(`${this._getEndpoint()}/${versionID}/${noteID}`);
}
}

export default NoteResource;
4 changes: 2 additions & 2 deletions packages/api-sdk/tests/client.unit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Client } from '@api-sdk/client';
import Fetch from '@api-sdk/fetch';
import { Analytics, APIKey, Diagram, Program, Project, PrototypeProgram, User, VariableState, Version } from '@api-sdk/resources';
import { Analytics, APIKey, Diagram, Note, Program, Project, PrototypeProgram, User, VariableState, Version } from '@api-sdk/resources';
import { expect } from 'chai';
import JWT from 'jsonwebtoken';

const CLIENT_RESOURCES = [Fetch, Diagram, Program, Project, Version, User, APIKey, Analytics, VariableState];
const CLIENT_RESOURCES = [Fetch, Diagram, Program, Project, Version, User, APIKey, Analytics, VariableState, Note];

const USER_HASH = 'UserHash_16chars';
const createClient = (authorization?: string) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/api-sdk/tests/index.unit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable dot-notation */
import { Client } from '@api-sdk/client';
import ApiSDK, { SGenerateClientParams, SParams } from '@api-sdk/index';
import { PublicClient } from '@api-sdk/publicclient';
import { PublicClient } from '@api-sdk/publicClient';
import { expect } from 'chai';
import JWT from 'jsonwebtoken';
import sinon from 'sinon';
Expand Down
49 changes: 49 additions & 0 deletions packages/api-sdk/tests/resources/note.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Note } from '@api-sdk/resources';
import { expect } from 'chai';
import sinon from 'sinon';

const NOTE = { id: 'qwe', text: 'text', mentions: [1, 2], meta: {} };

const createClient = () => {
const fetch = {
get: sinon.stub(),
post: sinon.stub(),
put: sinon.stub(),
patch: sinon.stub(),
delete: sinon.stub(),
};

const resource = new Note(fetch as any);

return {
fetch,
resource,
};
};

describe('NoteResource', () => {
afterEach(() => {
sinon.restore();
});

it('.upsert', async () => {
const { fetch, resource } = createClient();

fetch.put.resolves({ data: NOTE });

const data = await resource.upsert('1', NOTE as any);

expect(fetch.put.callCount).to.eql(1);
expect(fetch.put.args[0]).to.eql([`notes/1`, { note: NOTE }]);
expect(data).to.eql(NOTE);
});

it('.delete', async () => {
const { fetch, resource } = createClient();

await resource.delete('1', 'qwe');

expect(fetch.delete.callCount).to.eql(1);
expect(fetch.delete.args[0]).to.eql([`notes/1/qwe`]);
});
});
1 change: 1 addition & 0 deletions packages/base-types/src/models/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './command';
export * from './common';
export * from './intent';
export * from './node';
export * from './note';
export * from './prototype';
export * from './slot';
1 change: 1 addition & 0 deletions packages/base-types/src/models/base/intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export interface Intent {
name: string;
slots?: IntentSlot[];
inputs: IntentInput[];
noteID?: string;
}
24 changes: 24 additions & 0 deletions packages/base-types/src/models/base/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AnyRecord } from '@base-types/types';

export enum NoteType {
INTENT = 'INTENT',
}

export interface BaseNote {
id: string;
type: NoteType;
text: string;
meta?: AnyRecord;
mentions: number[];
}

export interface IntentNoteMeta {
intentID: string;
}

export interface IntentNote extends BaseNote {
type: NoteType.INTENT;
meta: IntentNoteMeta;
}

export type AnyNote = IntentNote;
3 changes: 2 additions & 1 deletion packages/base-types/src/models/version/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AnyRecord } from '@base-types/types';

import { BaseCommand, Intent, Slot, Variable } from '../base';
import { BaseCommand, BaseNote, Intent, Slot, Variable } from '../base';
import { Prototype } from './prototype';

export * from './prototype';
Expand Down Expand Up @@ -45,6 +45,7 @@ export interface Model<_PlatformData extends PlatformData, Command extends BaseC
rootDiagramID: string;

name: string;
notes?: Record<string, BaseNote>;
topics?: FolderItem[];
folders?: Record<string, Folder>;
variables: Variable[];
Expand Down

0 comments on commit 29c63ec

Please sign in to comment.