@botmock/client
Advanced tools
Comparing version 0.2.0 to 0.3.0
/// <reference types="node" /> | ||
import { Agent } from "https"; | ||
import { Client, FetchResult, ResourcesOptions, ProjectOptions, FetchResults } from "../types"; | ||
import { FetchResult, ResourcesOptions, ProjectOptions, FetchResults } from "../types"; | ||
export { Resource } from "../types"; | ||
export interface Config { | ||
token: string; | ||
token?: string; | ||
debug?: boolean; | ||
agent?: Agent; | ||
url?: string; | ||
} | ||
export default class ClientImpl<T = any> implements Client<T> { | ||
export declare enum VerboseMethod { | ||
GET_PROJECTS = "getProjects", | ||
GET_BOARDS = "getBoards", | ||
GET_TEAMS = "getTeams" | ||
} | ||
export default class Client { | ||
#private; | ||
debug: T; | ||
constructor(config: Config); | ||
constructor(config?: Config); | ||
withToken: (token: string) => Client; | ||
forTeams: (teamId: string[]) => Client; | ||
withProjectIds: (projectIds: string[]) => Client; | ||
getProjects(): Promise<import("../types").JSONResponse<any>[]>; | ||
getBoards(): Promise<import("../types").JSONResponse<any>[]>; | ||
getTeams(): Promise<import("../types").JSONResponse<any>[]>; | ||
getResources(opt: ResourcesOptions): Promise<FetchResult | FetchResult[]>; | ||
getProject(teamId: string, projectId: string): FetchResult; | ||
getProjects(options: ProjectOptions[]): FetchResults; | ||
getProjectsFromOptions(options: ProjectOptions[]): FetchResults; | ||
getTeam(teamId: string): FetchResult; | ||
getTeams(teamIds: Array<string>): FetchResults; | ||
getTeamsFromOptions(teamIds: Array<string>): FetchResults; | ||
getBoard(teamId: string, projectId: string, boardId: string): FetchResult; | ||
getBoards(teamId: string, projectId: string, boardIds: Array<string>): FetchResults; | ||
getBoardsFromOptions(teamId: string, projectId: string, boardIds: Array<string>): FetchResults; | ||
getIntents(teamId: string, projectId: string): FetchResult; | ||
@@ -22,0 +33,0 @@ getVariables(teamId: string, projectId: string): FetchResult; |
@@ -34,4 +34,9 @@ "use strict"; | ||
}; | ||
var _token, _url, _agent, _resourceMethodMap, _fetch; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var _state, _url, _logger, _agent, _methodDependencyGraph, _resourceMethodMap, _verifyRequirements, _fetch; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.VerboseMethod = void 0; | ||
const debug_1 = __importDefault(require("debug")); | ||
const node_fetch_1 = __importStar(require("node-fetch")); | ||
@@ -41,8 +46,26 @@ const types_1 = require("../types"); | ||
Object.defineProperty(exports, "Resource", { enumerable: true, get: function () { return types_2.Resource; } }); | ||
class ClientImpl { | ||
var Dependency; | ||
(function (Dependency) { | ||
Dependency["BOARD_IDS"] = "boardIds"; | ||
Dependency["PROJECT_IDS"] = "projectIds"; | ||
Dependency["TEAM_IDS"] = "teamIds"; | ||
Dependency["TOKEN"] = "token"; | ||
})(Dependency || (Dependency = {})); | ||
var VerboseMethod; | ||
(function (VerboseMethod) { | ||
VerboseMethod["GET_PROJECTS"] = "getProjects"; | ||
VerboseMethod["GET_BOARDS"] = "getBoards"; | ||
VerboseMethod["GET_TEAMS"] = "getTeams"; | ||
})(VerboseMethod = exports.VerboseMethod || (exports.VerboseMethod = {})); | ||
class Client { | ||
constructor(config) { | ||
this.debug = {}; | ||
_token.set(this, void 0); | ||
_state.set(this, {}); | ||
_url.set(this, "https://app.botmock.com/api/v2"); | ||
_logger.set(this, void 0); | ||
_agent.set(this, void 0); | ||
_methodDependencyGraph.set(this, new Map([ | ||
[VerboseMethod.GET_PROJECTS, [Dependency.PROJECT_IDS, Dependency.TEAM_IDS, Dependency.TOKEN]], | ||
[VerboseMethod.GET_BOARDS, [Dependency.BOARD_IDS, Dependency.PROJECT_IDS, Dependency.TEAM_IDS, Dependency.TOKEN]], | ||
[VerboseMethod.GET_TEAMS, [Dependency.PROJECT_IDS, Dependency.TEAM_IDS, Dependency.TOKEN]], | ||
])); | ||
_resourceMethodMap.set(this, new Map([ | ||
@@ -56,7 +79,40 @@ [types_1.Resource.PROJECT, "getProject"], | ||
])); | ||
this.withToken = (token) => { | ||
__classPrivateFieldGet(this, _state).token = token; | ||
return this; | ||
}; | ||
this.forTeams = (teamId) => { | ||
__classPrivateFieldGet(this, _state).teamIds = [...teamId]; | ||
return this; | ||
}; | ||
this.withProjectIds = (projectIds) => { | ||
__classPrivateFieldGet(this, _state).projectIds = [...projectIds]; | ||
return this; | ||
}; | ||
_verifyRequirements.set(this, (methodName) => { | ||
var _a; | ||
const requirements = (_a = __classPrivateFieldGet(this, _methodDependencyGraph).get(methodName)) !== null && _a !== void 0 ? _a : []; | ||
for (const req of requirements) { | ||
if (typeof __classPrivateFieldGet(this, _logger) !== "undefined") { | ||
__classPrivateFieldGet(this, _logger).log(`verifying ${req} in Client state`); | ||
} | ||
if (typeof __classPrivateFieldGet(this, _state)[req] === "undefined" || (Array.isArray(__classPrivateFieldGet(this, _state)[req]) && __classPrivateFieldGet(this, _state)[req].length === 0)) { | ||
throw new Error(`\`.${req}()\` must be called before \`${VerboseMethod[methodName]}\` in the chain`); | ||
} | ||
} | ||
}); | ||
_fetch.set(this, async (endpoint) => { | ||
if (endpoint.includes("undefined")) { | ||
throw new Error("missing project data"); | ||
} | ||
else if (!__classPrivateFieldGet(this, _state).token) { | ||
throw new Error("missing token"); | ||
} | ||
const fullURL = `${__classPrivateFieldGet(this, _url)}/${endpoint}`; | ||
if (typeof __classPrivateFieldGet(this, _logger) !== "undefined") { | ||
__classPrivateFieldGet(this, _logger).log(`fetching ${fullURL}`); | ||
} | ||
const res = await node_fetch_1.default(fullURL, { | ||
headers: { | ||
Authorization: `Bearer ${__classPrivateFieldGet(this, _token)}`, | ||
Authorization: `Bearer ${__classPrivateFieldGet(this, _state).token}`, | ||
Accept: "application/json", | ||
@@ -68,15 +124,48 @@ }, | ||
if (res.statusText !== types_1.StatusText.OK) { | ||
if (typeof __classPrivateFieldGet(this, _logger) !== "undefined") { | ||
__classPrivateFieldGet(this, _logger).log(` | ||
failed to fetch: | ||
${res} | ||
`); | ||
} | ||
throw new node_fetch_1.FetchError(res.statusText, "error"); | ||
} | ||
if (typeof __classPrivateFieldGet(this, _logger) !== "undefined") { | ||
__classPrivateFieldGet(this, _logger).log(`status: ${res.statusText}`); | ||
} | ||
const json = await res.json(); | ||
return json; | ||
}); | ||
__classPrivateFieldSet(this, _token, config.token); | ||
if (config.url) { | ||
__classPrivateFieldSet(this, _url, config.url); | ||
if (config === null || config === void 0 ? void 0 : config.debug) { | ||
__classPrivateFieldSet(this, _logger, debug_1.default); | ||
__classPrivateFieldGet(this, _logger).enabled("@botmock/client"); | ||
} | ||
if (typeof config.agent !== "undefined") { | ||
if (config === null || config === void 0 ? void 0 : config.token) { | ||
__classPrivateFieldGet(this, _state).token = config.token; | ||
} | ||
if (config === null || config === void 0 ? void 0 : config.agent) { | ||
__classPrivateFieldSet(this, _agent, config.agent); | ||
} | ||
if (config === null || config === void 0 ? void 0 : config.url) { | ||
__classPrivateFieldSet(this, _url, config.url); | ||
} | ||
} | ||
async getProjects() { | ||
__classPrivateFieldGet(this, _verifyRequirements).call(this, VerboseMethod.GET_PROJECTS); | ||
const [teamId] = __classPrivateFieldGet(this, _state).teamIds; | ||
const projectIds = __classPrivateFieldGet(this, _state).projectIds; | ||
return await Promise.all(projectIds.map(async (projectId) => await this.getProject(teamId, projectId))); | ||
} | ||
async getBoards() { | ||
__classPrivateFieldGet(this, _verifyRequirements).call(this, VerboseMethod.GET_BOARDS); | ||
const [teamId] = __classPrivateFieldGet(this, _state).teamIds; | ||
const [projectId] = __classPrivateFieldGet(this, _state).projectIds; | ||
const boardIds = __classPrivateFieldGet(this, _state).boardIds; | ||
return await Promise.all(boardIds.map(async (boardId) => await this.getBoard(teamId, projectId, boardId))); | ||
} | ||
async getTeams() { | ||
__classPrivateFieldGet(this, _verifyRequirements).call(this, VerboseMethod.GET_TEAMS); | ||
const teamIds = __classPrivateFieldGet(this, _state).teamIds; | ||
return await Promise.all(teamIds.map(async (teamId) => await this.getTeam(teamId))); | ||
} | ||
async getResources(opt) { | ||
@@ -105,3 +194,3 @@ let optIsArray = true; | ||
} | ||
async getProjects(options) { | ||
async getProjectsFromOptions(options) { | ||
let projectPromises = options.map((projectOptions) => { | ||
@@ -115,3 +204,3 @@ return this.getProject(projectOptions.teamId, projectOptions.projectId); | ||
} | ||
async getTeams(teamIds) { | ||
async getTeamsFromOptions(teamIds) { | ||
let teamPromises = teamIds.map((teamId) => { | ||
@@ -125,3 +214,3 @@ return this.getTeam(teamId); | ||
} | ||
async getBoards(teamId, projectId, boardIds) { | ||
async getBoardsFromOptions(teamId, projectId, boardIds) { | ||
let boardPromises = boardIds.map((boardId) => { | ||
@@ -142,4 +231,4 @@ return this.getBoard(teamId, projectId, boardId); | ||
} | ||
exports.default = ClientImpl; | ||
_token = new WeakMap(), _url = new WeakMap(), _agent = new WeakMap(), _resourceMethodMap = new WeakMap(), _fetch = new WeakMap(); | ||
exports.default = Client; | ||
_state = new WeakMap(), _url = new WeakMap(), _logger = new WeakMap(), _agent = new WeakMap(), _methodDependencyGraph = new WeakMap(), _resourceMethodMap = new WeakMap(), _verifyRequirements = new WeakMap(), _fetch = new WeakMap(); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
export { default as Client, Resource } from "./fetch"; | ||
export { default as Client, Resource, VerboseMethod } from "./fetch"; | ||
export * from "./types"; |
@@ -16,3 +16,4 @@ "use strict"; | ||
Object.defineProperty(exports, "Resource", { enumerable: true, get: function () { return fetch_1.Resource; } }); | ||
Object.defineProperty(exports, "VerboseMethod", { enumerable: true, get: function () { return fetch_1.VerboseMethod; } }); | ||
__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,2 @@ | ||
export interface Client<T = any> { | ||
debug: T; | ||
export interface Client { | ||
} | ||
@@ -4,0 +3,0 @@ export declare enum StatusText { |
{ | ||
"name": "@botmock/client", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -13,4 +13,4 @@ "contributors": [ | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"main": "lib/src/index.js", | ||
"types": "lib/src/index.d.ts", | ||
"publishConfig": { | ||
@@ -43,12 +43,14 @@ "access": "public" | ||
"dependencies": { | ||
"@types/debug": "^4.1.5", | ||
"@types/node-fetch": "2.5.7", | ||
"debug": "^4.1.1", | ||
"node-fetch": "2.6.0", | ||
"typescript": "3.9.5" | ||
"typescript": "3.9.7" | ||
}, | ||
"gitHead": "b4eac78d304bbf1769edc803881b2458f09bfc65", | ||
"devDependencies": { | ||
"@types/jest": "25.2.3", | ||
"jest": "26.0.1", | ||
"ts-jest": "26.1.1" | ||
"@types/jest": "26.0.5", | ||
"jest": "26.1.0", | ||
"ts-jest": "26.1.3" | ||
} | ||
} |
713
README.md
@@ -1,426 +0,443 @@ | ||
# @botmock/client | ||
# `@botmock/client` | ||
## Overview | ||
Botmock client package is used to **unlock data from Botmock projects**. | ||
This package is used to **extract data** from Botmock projects. | ||
Let's take a deeper dive and see what are those public methods that are exposed by Botmock Client and also what to expect from the response from these public methods. | ||
### Install | ||
## Install | ||
The package is available to install on npm. | ||
The package is available to install on npm. It is written in TypeScript, and comes bundled with type definitions. | ||
```bash | ||
npm install @botmock/client | ||
``` | ||
## Resources | ||
### Guide | ||
Resources are all the nuts and bolts of a given project. Resources includes project information, a board is part of a project, so its information which includes the information about blocks and its connections. A project belongs to a team so resources also give us team information. Our intents, entities and variables are essential part of a board, so resources also contains information about these three. | ||
The following guides assumes a [TypeScript](https://www.typescriptlang.org/v2/docs/handbook/typescript-in-5-minutes-func.html) environment with [LTS Node.js](https://nodejs.org/en/) installed. | ||
## Single Resource Requests | ||
> **Note**: You can bootstrap such an environment by running `npx @botmock/cli` | ||
Each of the following methods is publicly available on an instance of the named export `Client`: | ||
> **Note**: Tokens and ids are [created and accessed using the Botmock dashboard](http://help.botmock.com/en/articles/2334581-developer-api) | ||
```tsx | ||
const client = new Client({ token: process.env.TOKEN }); | ||
const project = await client.getProject(process.env.TEAM_ID, process.env.projectId); | ||
``` | ||
Let's walk through building a small Node.js app that fetches data from multiple Botmock projects. | ||
### `getProject(teamId: string, projectId: string): FetchResult` | ||
```typescript | ||
// index.ts | ||
import { Client } from "@botmock/client"; | ||
This method takes two parameters: `teamId` as a string and `projectId` which is also a string and then returns JSON for the information about the project as shown in a sample JSON below: | ||
const client = new Client(); | ||
```json | ||
{ | ||
"id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"name": "Kindly", | ||
"created_at": "2017-03-10 19:16:40", | ||
"updated_at": "2017-09-15 14:15:59", | ||
"type": "mock", | ||
"platform": "facebook" | ||
} | ||
(async () => { | ||
const projects = await client | ||
.withToken("YOUR_TOKEN") | ||
.forTeam("YOUR_TEAM_ID") | ||
.withProjectIds(["PROJECT_ID_1", "PROJECT_ID_2"]) | ||
.getProjects(); | ||
console.log(projects); // array of project objects | ||
})(); | ||
``` | ||
### `getProjects(ProjectOptions[]): FetchResults` | ||
Running `ts-node index.ts` should print an array of project objects. | ||
This method takes an array of objects containing a `teamId` property as a string and `projectId` property which is also a string. It then returns an array of JSON for the information about the projects as shown in a sample JSON below: | ||
## API | ||
```json | ||
[ | ||
{ | ||
"id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"name": "Kindly", | ||
"created_at": "2017-03-10 19:16:40", | ||
"updated_at": "2017-09-15 14:15:59", | ||
"type": "mock", | ||
"platform": "facebook" | ||
} | ||
] | ||
``` | ||
#### `Client` | ||
### `getTeam(teamId: string): FetchResult` | ||
##### Chaining methods | ||
This method takes in one parameter of `teamId` as string and returns the team information as shown in the following JSON below: | ||
Once a new instance of `Client` is made, the following **chaining methods** can be chained together to build requests. | ||
```json | ||
{ | ||
"id": 38881, | ||
"name": "First Team", | ||
"photo": "https://www.gravatar.com/avatar/00f062fb089de61b9a7e8033bc40109a.jpg?s=200&d=identicon", | ||
"created_at": { | ||
"date": "2017-01-10 20:45:21.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
} | ||
``` | ||
- `.withToken(string)` | ||
- `.forTeam(string)` | ||
- `.onBoard(string)` | ||
- `.withProjectIds(string[])` | ||
### `getTeams(teamIds: Array<string>): FetchResults` | ||
To execute requests, the following methods can be used at the end of a build chain. Listed below each of these **dispatch methods** are the required chaining methods. | ||
This method takes in a `teamIds` array that contains the team IDs as strings and returns the information for each team as shown in the following JSON below: | ||
- **`.getProjects()`** | ||
- `.withToken(string`)` | ||
- `.forTeam(string)` | ||
- `.withProjectIds(string[])` | ||
- **`.getBoards()`** | ||
- `.withToken(string`)` | ||
- `.forTeam(string)` | ||
- `.onBoard(string)` | ||
- `.withProjectIds(string[])` | ||
- **`.getTeams()`** | ||
- `.withToken()` | ||
- `.forTeam(string)` | ||
- `.withProjectIds(string[])` | ||
- **`.getPaths()`** | ||
- `.withToken(string`)` | ||
- `.forTeam(string)` | ||
- `.onBoard(string)` | ||
- `.withProjectIds(string[])` | ||
```json | ||
[ | ||
{ | ||
"id": 38881, | ||
"name": "First Team", | ||
"photo": "https://www.gravatar.com/avatar/00f062fb089de61b9a7e8033bc40109a.jpg?s=200&d=identicon", | ||
"created_at": { | ||
"date": "2017-01-10 20:45:21.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
}, | ||
{ | ||
"id": 38743, | ||
"name": "Second Team", | ||
"photo": "https://www.gravatar.com/avatar/00f062fb089de61b9a7e8033bc40109a.jpg?s=200&d=identicon", | ||
"created_at": { | ||
"date": "2017-01-20 20:45:21.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
} | ||
] | ||
##### `getResources` | ||
As an alternative to the chaining approach outlined above, resources can be fetched in a single method call. | ||
Returns a `Promise` which resolves an object containing payloads for each of `resources` provided in the options object. | ||
The options object contains the following. | ||
| Key name | Type | Description | | ||
| ----------- | ----------- | ----------- | | ||
| teamId | `string` | id of the team | | ||
| projectId | `string` | id of the project | | ||
| boardId | `string` | (optional) id of the board | | ||
| resources | `string[]` | array of "project", "team", "board", "intents", "variables", or "entities" | | ||
```typescript | ||
import { Client, Resource } from "@botmock/client"; | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const { project, team, board, intents, variables, entities } = await client.getResources({ | ||
teamId: "YOUR_TEAM_ID", | ||
projectId: "YOUR_PROJECT_ID", | ||
boardId: "YOUR_BOARD_ID", | ||
resources: [ | ||
Resource.PROJECT, | ||
Resource.TEAM, | ||
Resource.BOARD, | ||
Resource.INTENTS, | ||
Resource.VARIABLES, | ||
Resource.ENTITIES, | ||
] | ||
}); | ||
// Each destructured property above contains that slice of data from the Botmock project specified in the options object. | ||
})(); | ||
``` | ||
### `getBoard(teamId: string, projectId: string, boardId: string): FetchResult` | ||
The object returned by `getResources` can look like the following. | ||
GetBoard is a method that can be used to get all the board information if provided correct `teamId, projectId, boardId` all these parameters are expected to be strings. The sample JSON response is shown below: | ||
```json | ||
{ | ||
"project": { | ||
"id": "637882f0-93ab-11ea-8c14-4d6810dd96fc", | ||
"name": "complex", | ||
"type": "flow", | ||
"platform": "multi-channel", | ||
"channels": [ | ||
"generic" | ||
], | ||
"locales": [ | ||
"en" | ||
], | ||
"created_at": { | ||
"date": "2020-05-11 17:18:10.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"updated_at": { | ||
"date": "2020-06-26 16:39:01.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
}, | ||
"team": { | ||
"id": 1831, | ||
"name": "Botmock Team", | ||
"photo": "https://app.botmock.com/storage/profiles/Wp5VXSQ4LUTSkfLKlH7IIOgXmkDcQh5OXhaghtFv.png", | ||
"created_at": { | ||
"date": "2017-08-12 03:38:19.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
}, | ||
"board": { | ||
"board": { | ||
"root_messages": [ | ||
"16f1c520-05c6-11e7-bc31-5554df9b496b" | ||
], | ||
"messages": [ | ||
"root_messages": [ | ||
"637a5d70-93ab-11ea-a217-33512b13de41" | ||
], | ||
"messages": [ | ||
{ | ||
"message_id": "637a5f60-93ab-11ea-a478-8debb9e9d485", | ||
"message_type": "response", | ||
"next_message_ids": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"message_type": "response", | ||
"next_message_ids": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"action": "*", | ||
"intent": "", | ||
"conditional": false | ||
} | ||
], | ||
"previous_message_ids": [], | ||
"is_root": true, | ||
"payload": { | ||
"en": { | ||
"facebook": { | ||
"blocks": [ | ||
[] | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"message_id": "27074d90-b3df-44b3-8e4e-c9b8802dad61", | ||
"action": "*", | ||
"intent": { | ||
"label": "give_party_size", | ||
"value": "da7ccf40-b7ca-11ea-8ff6-c37e8170426d" | ||
}, | ||
"conditional": { | ||
"id": "g-DsAWYCMF1acBzPXVRVi0z", | ||
"rules": [], | ||
"combinator": "and", | ||
"not": false | ||
} | ||
} | ||
], | ||
"previous_message_ids": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"message_type": "response", | ||
"next_message_ids": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"action": "*", | ||
"intent": "", | ||
"conditional": false | ||
} | ||
], | ||
"previous_message_ids": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"action": "*" | ||
} | ||
], | ||
"is_root": false, | ||
"payload": { | ||
"en": { | ||
"facebook": { | ||
"blocks": [ | ||
{ | ||
"component_type": "text", | ||
"nodeName": "Bot Says", | ||
"text": "Your bot reply goes here...", | ||
"buttons": [], | ||
"delay": 2000, | ||
"workflow_index": 0 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
] | ||
"message_id": "637a5d70-93ab-11ea-a217-33512b13de41", | ||
"action": "*" | ||
} | ||
], | ||
"is_root": false, | ||
"node_name": "Bot Says", | ||
"payload": { | ||
"en": { | ||
"generic": { | ||
"blocks": [ | ||
{ | ||
"component_type": "text", | ||
"nodeName": "Bot Says", | ||
"context": [ | ||
{ | ||
"id": "a0a11bd0-b7ca-11ea-a1cf-4155954aa7c8", | ||
"name": "%name%", | ||
"type": "text", | ||
"entity": "", | ||
"default_value": "", | ||
"start_index": 3 | ||
} | ||
], | ||
"text": "Hi %name%, how many for your reservation?", | ||
"audio_file": "", | ||
"ssml": "", | ||
"delay": 2000, | ||
"alternate_replies": [ | ||
"one" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"slots": { | ||
"34cb53e0-898e-11ea-a937-3f5c9b13d9b8": [] | ||
}, | ||
"variables": [], | ||
"slots": {}, | ||
"variables": [ | ||
{ | ||
"id": "a0a11bd0-b7ca-11ea-a1cf-4155954aa7c8", | ||
"name": "name", | ||
"default_value": "", | ||
"type": "text", | ||
"entity": "", | ||
"created_at": { | ||
"date": "2020-06-26 16:32:29.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"updated_at": { | ||
"date": "2020-06-26 16:32:29.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
} | ||
], | ||
"created_at": { | ||
"date": "2020-04-28 20:24:05.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
"date": "2020-05-11 17:18:10.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"updated_at": { | ||
"date": "2020-04-30 17:02:40.000000", | ||
"date": "2020-06-26 16:36:24.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
}, | ||
"intents": [ | ||
{ | ||
"id": "1fbe2430-b7cb-11ea-86a3-61a8fe026c9c", | ||
"name": "done", | ||
"utterances": { | ||
"en": [ | ||
{ | ||
"text": "done", | ||
"variables": [] | ||
} | ||
] | ||
}, | ||
"created_at": { | ||
"date": "2020-06-26 16:36:02.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"updated_at": { | ||
"date": "2020-06-26 16:36:11.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"is_global": false, | ||
"slots": [] | ||
} | ||
], | ||
"variables": [ | ||
{ | ||
"id": "a0a11bd0-b7ca-11ea-a1cf-4155954aa7c8", | ||
"name": "name", | ||
"default_value": "", | ||
"type": "text", | ||
"entity": "", | ||
"created_at": { | ||
"date": "2020-06-26 16:32:29.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"updated_at": { | ||
"date": "2020-06-26 16:32:29.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
} | ||
], | ||
"entities": [] | ||
} | ||
``` | ||
### `getBoards(teamId: string, projectId: string, boardId: string): FetchResult` | ||
##### `getProject` | ||
This method can be used in the same way as the `getBoard` method, but this method allows for retrieving multiple boards from a single project. You must pass it a `teamId` as a string, a `projectId` string, and `boardIds` as an array of strings. The sample JSON response is shown below: | ||
Returns a `Promise` which resolves an object containing high-level project data. | ||
```json | ||
[ | ||
{ | ||
"board": { | ||
"root_messages": [ | ||
"16f1c520-05c6-11e7-bc31-5554df9b496b" | ||
], | ||
"messages": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"message_type": "response", | ||
"next_message_ids": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"action": "*", | ||
"intent": "", | ||
"conditional": false | ||
} | ||
], | ||
"previous_message_ids": [], | ||
"is_root": true, | ||
"payload": { | ||
"en": { | ||
"facebook": { | ||
"blocks": [ | ||
[] | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"message_type": "response", | ||
"next_message_ids": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"action": "*", | ||
"intent": "", | ||
"conditional": false | ||
} | ||
], | ||
"previous_message_ids": [ | ||
{ | ||
"message_id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"action": "*" | ||
} | ||
], | ||
"is_root": false, | ||
"payload": { | ||
"en": { | ||
"facebook": { | ||
"blocks": [ | ||
{ | ||
"component_type": "text", | ||
"nodeName": "Bot Says", | ||
"text": "Your bot reply goes here...", | ||
"buttons": [], | ||
"delay": 2000, | ||
"workflow_index": 0 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
] | ||
}, | ||
"slots": { | ||
"34cb53e0-898e-11ea-a937-3f5c9b13d9b8": [] | ||
}, | ||
"variables": [], | ||
"created_at": { | ||
"date": "2020-04-28 20:24:05.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"updated_at": { | ||
"date": "2020-04-30 17:02:40.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
} | ||
] | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const project = await client.getProject("YOUR_TEAM_ID", "YOUR_PROJECT_ID"); | ||
// `project` contains the project data specified by the team id and project id passed to the method. | ||
})(); | ||
``` | ||
### `getIntents(teamId: string, projectId: string): FetchResult` | ||
##### `getProjects` | ||
This method takes in a `teamId` and `projectId` and returns all the intents of a specific project. It returns an array of intent object and each object consists of an intent `id`, `name`, list of `utterances` and some meta information like `created_at` and `updated_at` | ||
Returns a `Promise` which resolves an array of objects containing high-level project data. | ||
```json | ||
[ | ||
{ | ||
"id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"name": "Order Pizza", | ||
"utterances": { | ||
"en": [ | ||
{ | ||
"text": "I want to order pizza", | ||
"variables": [] | ||
}, | ||
{ | ||
"text": "Order pizza", | ||
"variables": [] | ||
} | ||
] | ||
}, | ||
"created_at": { | ||
"date": "2020-04-28 20:24:05.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"updated_at": { | ||
"date": "2020-04-28 20:24:05.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"is_global": false, | ||
"slots": [] | ||
} | ||
] | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const projects = await client.getProjects([{ teamId: "YOUR_TEAM_ID", projectId: "YOUR_PROJECT_ID" }]); | ||
// `projects` contains the project data specified by the series of team ids and project ids passed to the method. | ||
})(); | ||
``` | ||
### `getEntities(teamId: string, projectId: string): FetchResult` | ||
##### `getTeam` | ||
This method takes in same as getIntents method a `teamId` and `projectId` and returns all the entities of a specific project. The sample JSON is shown below. | ||
Returns a `Promise` which resolves an object containing high-level team data. | ||
```json | ||
[ | ||
{ | ||
"id": "16f1c520-05c6-11e7-bc31-5554df9b496b", | ||
"name": "orderTime", | ||
"data": { | ||
"en": [ | ||
{ | ||
"value": "lunch", | ||
"synonyms": [ | ||
"brunch" | ||
] | ||
}, | ||
{ | ||
"value": "breakfast", | ||
"synonyms": [] | ||
} | ||
] | ||
}, | ||
"created_at": { | ||
"date": "2020-04-30 17:36:10.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
}, | ||
"updated_at": { | ||
"date": "2020-04-30 17:36:10.000000", | ||
"timezone_type": 3, | ||
"timezone": "UTC" | ||
} | ||
} | ||
] | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const team = await client.getTeam("YOUR_TEAM_ID"); | ||
// `team` contains the team data specified by the team id passed to the method. | ||
})(); | ||
``` | ||
## Batch Requests | ||
##### `getTeams` | ||
An `Enum` containing all resource names can be imported as follows: | ||
Returns a `Promise` which resolves an array of objects containing high-level team data. | ||
```tsx | ||
import { Resource } from "@botmock/client"; | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const teams = await client.getTeams(["YOUR_TEAM_ID"]); | ||
// `teams` contains the team data specified by the team ids passed to the method. | ||
})(); | ||
``` | ||
```tsx | ||
export enum Resource { | ||
PROJECT = "project", | ||
TEAM = "team", | ||
BOARD = "board", | ||
INTENTS = "intents", | ||
VARIABLES = "variables", | ||
ENTITIES = "entities", | ||
} | ||
##### `getBoard` | ||
Returns a `Promise` which resolves an object describing the specified board. | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const board = await client.getBoard("YOUR_TEAM_ID", "YOUR_PROJECT_ID", "YOUR_BOARD_ID"); | ||
// `board` contains the board data specified by the ids passed to the method. | ||
})(); | ||
``` | ||
This import gives us access to names of all available resources. When we call `getResources` its on us which resources we need. so we need to pass then in the parameters of `getResources` method as shown below. It can all but it can also be a few on them. | ||
##### `getBoards` | ||
### `getResources` method | ||
Returns a `Promise` which resolves an array of objects describing the specified boards. | ||
This public method can be used to fetch an array of **resource names** from a given project: | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
```tsx | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const boards = await client.getBoards("YOUR_TEAM_ID", "YOUR_PROJECT_ID", ["YOUR_BOARD_ID_1"]); | ||
// `boards` contains the board data specified by the ids passed to the method. | ||
})(); | ||
``` | ||
##### `getIntents` | ||
Returns a `Promise` which resolves an array of objects containing intent data for the specified project. | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
const client = new Client({ token: process.env.TOKEN }); | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
// It can be passed a single options object, and will return an object with the project's resources | ||
const { intents, project } = await client.getResources({ | ||
resources: [Resource.INTENTS, Resource.PROJECT], // Here we are going to only get two resources | ||
teamId: "1", | ||
projectId: "1", | ||
boardId: "1", | ||
}); | ||
const intents = await client.getIntents("YOUR_TEAM_ID", "YOUR_PROJECT_ID"); | ||
// `intents` contains the intent data specified by the team id and project id passed to the method. | ||
})(); | ||
// Or it can be passed an array of options, and will return an array of resource objects | ||
const [firstProjectResources, secondProjectResources] = await client.getResources( | ||
[ | ||
{ | ||
resources: [Resource.INTENTS, Resource.PROJECT], | ||
teamId: "1", | ||
projectId: "1", | ||
boardId: "1", | ||
}, | ||
{ | ||
resources: [Resource.INTENTS, Resource.PROJECT], | ||
teamId: "2", | ||
projectId: "2", | ||
boardId: "2", | ||
} | ||
] | ||
); | ||
``` | ||
##### `getVariables` | ||
Returns a `Promise` which resolves an array of objects containing variables data for the specified project. | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const variables = await client.getVariables("YOUR_TEAM_ID", "YOUR_PROJECT_ID"); | ||
// `variables` contains the variables data specified by the team id and project id passed to the method. | ||
})(); | ||
``` | ||
`client.getResources` promise resolves with an object containing keys for each of the elements of `resources` array. | ||
##### `getEntities` | ||
## Reporting Issues | ||
Returns a `Promise` which resolves an array of objects containing entities data for the specified project. | ||
If you would like to report an issue please feel free to send an email to [help@botmock.com](mailto:help@botmock.com) | ||
```typescript | ||
import { Client } from "@botmock/client"; | ||
Thanks for using Botmock! Happy Prototyping 😎 😎 👩🏻🎨 👨🏻🎨 😎 😎 | ||
const client = new Client({ token: "YOUR_TOKEN" }); | ||
(async () => { | ||
const entities = await client.getVariables("YOUR_TEAM_ID", "YOUR_PROJECT_ID"); | ||
// `entities` contains the entities data specified by the team id and project id passed to the method. | ||
})(); | ||
``` |
@@ -1,2 +0,2 @@ | ||
export { default as Client, Resource } from "./fetch"; | ||
export { Client as default, Client } from "./api"; | ||
export * from "./types"; |
@@ -1,5 +0,31 @@ | ||
export interface Client<T = any> { | ||
debug: T; | ||
import { Agent } from "https"; | ||
export interface Config { | ||
token?: string; | ||
debug?: boolean; | ||
agent?: Agent; | ||
url?: string; | ||
} | ||
export enum ChainMethod { | ||
WITH_TOKEN = "withToken", | ||
WITH_PROJECT_IDS = "withProjectIds", | ||
FOR_TEAM = "forTeam", | ||
ON_BOARD = "onBoard", | ||
} | ||
export enum DispatchMethod { | ||
GET_PROJECTS = "getProjects", | ||
GET_BOARDS = "getBoards", | ||
GET_TEAMS = "getTeams", | ||
GET_PATHS = "getPaths", | ||
} | ||
export enum Dep { | ||
BOARD_IDS = "boardIds", | ||
PROJECT_IDS = "projectIds", | ||
TEAM_IDS = "teamIds", | ||
TOKEN = "token", | ||
} | ||
export enum StatusText { | ||
@@ -6,0 +32,0 @@ OK = "OK", |
import { describe, expect, test, beforeAll, jest, afterAll } from "@jest/globals"; | ||
import { Client, Resource } from "../src"; | ||
import output from "./mocks/output.json"; | ||
import { Client, Resource, DispatchMethod } from "../src"; | ||
import getResourcesOutput from "./mocks/output.json"; | ||
beforeAll(() => { | ||
// @ts-ignore | ||
jest.spyOn(Client.prototype, "getResources").mockImplementation(() => output); | ||
describe("verbose mode", () => { | ||
let client!: Client; | ||
beforeAll(() => { | ||
jest.spyOn(Client.prototype, DispatchMethod.GET_PROJECTS).mockImplementation(() => ([{ id: "" }, { id: "" }] as any)); | ||
jest.spyOn(Client.prototype, DispatchMethod.GET_BOARDS).mockImplementation(() => ([{ id: "" }] as any)); | ||
jest.spyOn(Client.prototype, DispatchMethod.GET_TEAMS).mockImplementation(() => ([{ id: "" }] as any)); | ||
client = new Client(); | ||
}); | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test("get*", async () => { | ||
const projects = await client.withToken("_").forTeam("_").withProjectIds(["_", "_"]).getProjects(); | ||
const boards = await client.withToken("_").forTeam("_").withProjectIds(["_"]).getBoards(); | ||
const teams = await client.withToken("_").forTeam("_").getTeams(); | ||
expect(projects).toHaveLength(2); | ||
expect(teams).toEqual([{ id: "" }]); | ||
expect(boards).toEqual([{ id: "" }]); | ||
}); | ||
}); | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
describe("getResources", () => { | ||
const client = new Client({ token: "" }); | ||
let client!: Client; | ||
beforeAll(() => { | ||
jest.spyOn(Client.prototype, "getResources").mockImplementation(() => getResourcesOutput as any); | ||
client = new Client({ token: "" }); | ||
}); | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test("output has correct shape", async () => { | ||
@@ -17,0 +36,0 @@ const resources = await client.getResources({ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
82344
27
1192
444
5
1
+ Added@types/debug@^4.1.5
+ Addeddebug@^4.1.1
+ Added@types/debug@4.1.12(transitive)
+ Added@types/ms@2.1.0(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addedtypescript@3.9.7(transitive)
- Removedtypescript@3.9.5(transitive)
Updatedtypescript@3.9.7