chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import axiosRetry from 'axios-retry'
|
||||
import * as common from '../common'
|
||||
import * as gen from '../gen/admin'
|
||||
import * as types from '../types'
|
||||
|
||||
type IClient = common.types.Simplify<gen.Client>
|
||||
export type Operation = common.types.Operation<IClient>
|
||||
export type ClientInputs = common.types.Inputs<IClient>
|
||||
export type ClientOutputs = common.types.Outputs<IClient>
|
||||
|
||||
export type ClientProps = common.types.CommonClientProps & {
|
||||
workspaceId?: string
|
||||
token: string
|
||||
}
|
||||
|
||||
export class Client extends gen.Client {
|
||||
public readonly config: Readonly<types.ClientConfig>
|
||||
|
||||
public constructor(clientProps: ClientProps) {
|
||||
const clientConfig = common.config.getClientConfig(clientProps)
|
||||
const axiosInstance = common.axios.createAxiosInstance(clientConfig)
|
||||
|
||||
super(axiosInstance, {
|
||||
toApiError: common.errors.toApiError,
|
||||
})
|
||||
|
||||
if (clientProps.retry) {
|
||||
axiosRetry(axiosInstance, clientProps.retry)
|
||||
}
|
||||
|
||||
this.config = clientConfig
|
||||
}
|
||||
|
||||
public get list() {
|
||||
type ListInputs = common.types.ListInputs<IClient>
|
||||
return {
|
||||
publicIntegrations: (props: ListInputs['listPublicIntegrations']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listPublicIntegrations({ nextToken, ...props }).then((r) => ({ ...r, items: r.integrations }))
|
||||
),
|
||||
bots: (props: ListInputs['listBots']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listBots({ nextToken, ...props }).then((r) => ({ ...r, items: r.bots }))
|
||||
),
|
||||
botIssues: (props: ListInputs['listBotIssues']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listBotIssues({ nextToken, ...props }).then((r) => ({ ...r, items: r.issues }))
|
||||
),
|
||||
workspaces: (props: ListInputs['listWorkspaces']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listWorkspaces({ nextToken, ...props }).then((r) => ({ ...r, items: r.workspaces }))
|
||||
),
|
||||
publicWorkspaces: (props: ListInputs['listPublicWorkspaces']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listPublicWorkspaces({ nextToken, ...props }).then((r) => ({ ...r, items: r.workspaces }))
|
||||
),
|
||||
workspaceMembers: (props: ListInputs['listWorkspaceMembers']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listWorkspaceMembers({ nextToken, ...props }).then((r) => ({ ...r, items: r.members }))
|
||||
),
|
||||
integrations: (props: ListInputs['listIntegrations']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listIntegrations({ nextToken, ...props }).then((r) => ({ ...r, items: r.integrations }))
|
||||
),
|
||||
interfaces: (props: ListInputs['listInterfaces']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listInterfaces({ nextToken, ...props }).then((r) => ({ ...r, items: r.interfaces }))
|
||||
),
|
||||
activities: (props: ListInputs['listActivities']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listActivities({ nextToken, ...props }).then((r) => ({ ...r, items: r.activities }))
|
||||
),
|
||||
usageActivity: (props: ListInputs['listUsageActivity']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listUsageActivity({ nextToken, ...props }).then((r) => ({ ...r, items: r.data }))
|
||||
),
|
||||
usageActivityDaily: (props: ListInputs['listUsageActivityDaily']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listUsageActivityDaily({ nextToken, ...props }).then((r) => ({ ...r, items: r.data }))
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import axiosRetry from 'axios-retry'
|
||||
import * as common from '../common'
|
||||
import * as gen from '../gen/billing'
|
||||
import * as types from '../types'
|
||||
|
||||
type IClient = common.types.Simplify<gen.Client>
|
||||
export type Operation = common.types.Operation<IClient>
|
||||
export type ClientInputs = common.types.Inputs<IClient>
|
||||
export type ClientOutputs = common.types.Outputs<IClient>
|
||||
|
||||
export type ClientProps = common.types.CommonClientProps & {
|
||||
workspaceId: string
|
||||
token: string
|
||||
}
|
||||
|
||||
export class Client extends gen.Client {
|
||||
public readonly config: Readonly<types.ClientConfig>
|
||||
|
||||
public constructor(clientProps: ClientProps) {
|
||||
const clientConfig = common.config.getClientConfig(clientProps)
|
||||
const axiosInstance = common.axios.createAxiosInstance(clientConfig)
|
||||
|
||||
super(axiosInstance, {
|
||||
toApiError: common.errors.toApiError,
|
||||
})
|
||||
|
||||
if (clientProps.retry) {
|
||||
axiosRetry(axiosInstance, clientProps.retry)
|
||||
}
|
||||
|
||||
this.config = clientConfig
|
||||
}
|
||||
|
||||
public get list() {
|
||||
type ListInputs = common.types.ListInputs<IClient>
|
||||
return {
|
||||
listInvoices: (props: ListInputs['listInvoices']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listInvoices({ nextToken, ...props }).then((r) => ({ ...r, items: r.invoices }))
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import * as axios from 'axios'
|
||||
import * as consts from './consts'
|
||||
import * as interceptors from './debug-interceptors'
|
||||
import * as types from './types'
|
||||
|
||||
const createAxios = (config: types.ClientConfig): axios.AxiosRequestConfig => ({
|
||||
baseURL: config.apiUrl,
|
||||
headers: config.headers,
|
||||
withCredentials: config.withCredentials,
|
||||
timeout: config.timeout,
|
||||
maxBodyLength: consts.maxBodyLength,
|
||||
maxContentLength: consts.maxContentLength,
|
||||
httpAgent: consts.httpAgent,
|
||||
httpsAgent: consts.httpsAgent,
|
||||
})
|
||||
|
||||
export const createAxiosInstance = (config: types.ClientConfig): axios.AxiosInstance => {
|
||||
const axiosConfig = createAxios(config)
|
||||
const axiosInstance = axios.default.create(axiosConfig)
|
||||
|
||||
if (config.debug) {
|
||||
interceptors.addDebugInterceptors(axiosInstance)
|
||||
}
|
||||
|
||||
return axiosInstance
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { isBrowser, isNode } from 'browser-or-node'
|
||||
import * as types from './types'
|
||||
|
||||
const defaultApiUrl = 'https://api.botpress.cloud'
|
||||
const defaultTimeout = 60_000
|
||||
const defaultDebug = false
|
||||
|
||||
const apiUrlEnvName = 'BP_API_URL'
|
||||
const botIdEnvName = 'BP_BOT_ID'
|
||||
const integrationIdEnvName = 'BP_INTEGRATION_ID'
|
||||
const workspaceIdEnvName = 'BP_WORKSPACE_ID'
|
||||
const tokenEnvName = 'BP_TOKEN'
|
||||
|
||||
type AnyClientProps = types.CommonClientProps & {
|
||||
integrationId?: string
|
||||
integrationAlias?: string
|
||||
workspaceId?: string
|
||||
botId?: string
|
||||
token?: string
|
||||
}
|
||||
|
||||
export function getClientConfig(clientProps: AnyClientProps): types.ClientConfig {
|
||||
const props = readEnvConfig(clientProps)
|
||||
|
||||
let headers: Record<string, string | string[]> = {}
|
||||
|
||||
if (props.workspaceId) {
|
||||
headers['x-workspace-id'] = props.workspaceId
|
||||
}
|
||||
|
||||
if (props.botId) {
|
||||
headers['x-bot-id'] = props.botId
|
||||
}
|
||||
|
||||
if (props.integrationId) {
|
||||
headers['x-integration-id'] = props.integrationId
|
||||
}
|
||||
|
||||
if (props.integrationAlias) {
|
||||
headers['x-integration-alias'] = props.integrationAlias
|
||||
}
|
||||
|
||||
if (props.token) {
|
||||
headers.Authorization = `Bearer ${props.token}`
|
||||
}
|
||||
|
||||
headers = {
|
||||
...headers,
|
||||
...props.headers,
|
||||
}
|
||||
|
||||
const apiUrl = props.apiUrl ?? defaultApiUrl
|
||||
const timeout = props.timeout ?? defaultTimeout
|
||||
const debug = props.debug ?? defaultDebug
|
||||
|
||||
return {
|
||||
apiUrl,
|
||||
timeout,
|
||||
withCredentials: isBrowser,
|
||||
headers,
|
||||
debug,
|
||||
}
|
||||
}
|
||||
|
||||
function readEnvConfig(props: AnyClientProps): AnyClientProps {
|
||||
if (isBrowser) {
|
||||
return getBrowserConfig(props)
|
||||
}
|
||||
|
||||
if (isNode) {
|
||||
return getNodeConfig(props)
|
||||
}
|
||||
|
||||
return props
|
||||
}
|
||||
|
||||
function getNodeConfig(props: AnyClientProps): AnyClientProps {
|
||||
const config: AnyClientProps = {
|
||||
...props,
|
||||
apiUrl: props.apiUrl ?? process.env[apiUrlEnvName],
|
||||
botId: props.botId ?? process.env[botIdEnvName],
|
||||
integrationId: props.integrationId ?? process.env[integrationIdEnvName],
|
||||
integrationAlias: props.integrationAlias,
|
||||
workspaceId: props.workspaceId ?? process.env[workspaceIdEnvName],
|
||||
}
|
||||
|
||||
const token = config.token ?? process.env[tokenEnvName]
|
||||
|
||||
if (token) {
|
||||
config.token = token
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
function getBrowserConfig(props: AnyClientProps): AnyClientProps {
|
||||
return props
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { isNode } from 'browser-or-node'
|
||||
import http from 'http'
|
||||
import https from 'https'
|
||||
|
||||
const _100mb = 100 * 1024 * 1024
|
||||
|
||||
export const maxBodyLength = _100mb
|
||||
export const maxContentLength = _100mb
|
||||
export const httpAgent = isNode && http && http.Agent ? new http.Agent({ keepAlive: true }) : undefined
|
||||
export const httpsAgent = isNode && https && https.Agent ? new https.Agent({ keepAlive: true }) : undefined
|
||||
@@ -0,0 +1,109 @@
|
||||
import * as axios from 'axios'
|
||||
import { isNode } from 'browser-or-node'
|
||||
let randomUUID: () => string
|
||||
if (isNode) {
|
||||
randomUUID = require('crypto').randomUUID
|
||||
} else if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
||||
randomUUID = () => crypto.randomUUID()
|
||||
} else {
|
||||
randomUUID = () => Math.random().toString(36).substring(2, 15)
|
||||
}
|
||||
|
||||
type AxiosRequestConfigWithMetadata<T = unknown> = {
|
||||
headers: axios.AxiosRequestHeaders
|
||||
metadata?: {
|
||||
id?: string
|
||||
startTime?: number
|
||||
}
|
||||
} & axios.AxiosRequestConfig<T>
|
||||
|
||||
type AxiosResponseWithMetadata<T = unknown, D = unknown> = {
|
||||
config: AxiosRequestConfigWithMetadata<D>
|
||||
} & axios.AxiosResponse<T, D>
|
||||
|
||||
type AxiosErrorWithMetadata<T = unknown, D = unknown> = {
|
||||
config: AxiosRequestConfigWithMetadata<D>
|
||||
} & axios.AxiosError<T, D>
|
||||
|
||||
export const addDebugInterceptors = (axiosInstance: axios.AxiosInstance) => {
|
||||
axiosInstance.interceptors.request.use((config: AxiosRequestConfigWithMetadata) => {
|
||||
config.metadata = { startTime: new Date().getTime(), id: randomUUID() }
|
||||
console.debug(_formatRequestLog(config))
|
||||
return config
|
||||
})
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response) => {
|
||||
console.debug(_formatResponseLog(response))
|
||||
return response
|
||||
},
|
||||
|
||||
(error) => {
|
||||
console.debug(_formatErrorLog(error))
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const _formatRequestLog = (config: AxiosRequestConfigWithMetadata): string => {
|
||||
const { method, url, headers, data } = config
|
||||
|
||||
const fullUrl = config.baseURL ? new URL(url!, config.baseURL).toString() : url
|
||||
|
||||
return (
|
||||
'REQUEST: ' +
|
||||
JSON.stringify({
|
||||
method: method?.toUpperCase(),
|
||||
url: fullUrl,
|
||||
timestamp: new Date().toISOString(),
|
||||
requestId: config.metadata?.id,
|
||||
headers,
|
||||
body: data,
|
||||
}) +
|
||||
'\n'
|
||||
)
|
||||
}
|
||||
|
||||
const _formatResponseLog = (response: AxiosResponseWithMetadata): string => {
|
||||
const { config, status, headers, data } = response
|
||||
const duration = _formatDuration(response)
|
||||
const fullUrl = config.baseURL ? new URL(response.config.url!, config.baseURL).toString() : response.config.url
|
||||
|
||||
return (
|
||||
'RESPONSE: ' +
|
||||
JSON.stringify({
|
||||
method: config.method?.toUpperCase(),
|
||||
status,
|
||||
url: fullUrl,
|
||||
timestamp: new Date().toISOString(),
|
||||
requestId: config.metadata?.id,
|
||||
duration,
|
||||
headers,
|
||||
body: data,
|
||||
}) +
|
||||
'\n'
|
||||
)
|
||||
}
|
||||
|
||||
const _formatErrorLog = (error: AxiosErrorWithMetadata): string => {
|
||||
const duration = error ? _formatDuration(error) : 'N/A'
|
||||
const fullUrl = error.config.baseURL ? new URL(error.config.url!, error.config.baseURL).toString() : error.config.url
|
||||
|
||||
return (
|
||||
'ERROR: ' +
|
||||
JSON.stringify({
|
||||
status: error.code,
|
||||
url: fullUrl,
|
||||
timestamp: new Date().toISOString(),
|
||||
requestId: error.config.metadata?.id ?? 'N/A',
|
||||
duration,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const _formatDuration = (response: AxiosResponseWithMetadata | AxiosErrorWithMetadata) => {
|
||||
const startTime = response.config.metadata?.startTime
|
||||
const endTime = new Date().getTime()
|
||||
const duration = startTime ? `${endTime - startTime}ms` : 'N/A'
|
||||
return duration
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import axios from 'axios'
|
||||
import * as errors from '../errors'
|
||||
|
||||
export const toApiError = (err: unknown): Error => {
|
||||
if (axios.isAxiosError(err) && err.response?.data) {
|
||||
return errors.errorFrom(err.response.data)
|
||||
}
|
||||
return errors.errorFrom(err)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export * as consts from './consts'
|
||||
export * as types from './types'
|
||||
export * as config from './config'
|
||||
export * as listing from './listing'
|
||||
export * as axios from './axios'
|
||||
export * as errors from './errors'
|
||||
@@ -0,0 +1,29 @@
|
||||
export type PageLister<R> = (t: { nextToken?: string }) => Promise<{ items: R[]; meta: { nextToken?: string } }>
|
||||
export class AsyncCollection<T> {
|
||||
public constructor(private _list: PageLister<T>) {}
|
||||
|
||||
public async *[Symbol.asyncIterator]() {
|
||||
let nextToken: string | undefined
|
||||
do {
|
||||
const { items, meta } = await this._list({ nextToken })
|
||||
nextToken = meta.nextToken
|
||||
for (const item of items) {
|
||||
yield item
|
||||
}
|
||||
} while (nextToken)
|
||||
}
|
||||
|
||||
public async collect(props: { limit?: number } = {}) {
|
||||
const limit = props.limit ?? Number.POSITIVE_INFINITY
|
||||
const arr: T[] = []
|
||||
let count = 0
|
||||
for await (const item of this) {
|
||||
arr.push(item)
|
||||
count++
|
||||
if (count >= limit) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return arr
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import * as types from '../types'
|
||||
|
||||
export * from '../types'
|
||||
|
||||
export type CommonClientProps = {
|
||||
apiUrl?: string
|
||||
timeout?: number
|
||||
headers?: types.Headers
|
||||
retry?: types.RetryConfig
|
||||
debug?: boolean
|
||||
}
|
||||
|
||||
export type Cast<T, U> = T extends U ? T : U
|
||||
export type AsyncFunc = (...args: any[]) => Promise<any>
|
||||
|
||||
type SimplifyTuple<T> = T extends [...infer A] ? { [K in keyof A]: Simplify<A[K]> } : never
|
||||
type SimplifyObject<T extends object> = T extends infer O ? { [K in keyof O]: Simplify<O[K]> } : never
|
||||
export type Simplify<T> = T extends (...args: infer A) => infer R
|
||||
? (...args: SimplifyTuple<A>) => Simplify<R>
|
||||
: T extends Array<infer E>
|
||||
? Array<Simplify<E>>
|
||||
: T extends ReadonlyArray<infer E>
|
||||
? ReadonlyArray<Simplify<E>>
|
||||
: T extends Promise<infer R>
|
||||
? Promise<Simplify<R>>
|
||||
: T extends Buffer
|
||||
? Buffer
|
||||
: T extends object
|
||||
? SimplifyObject<T>
|
||||
: T
|
||||
|
||||
export type Operation<C extends Record<string, AsyncFunc>> = Simplify<
|
||||
keyof {
|
||||
[K in keyof C as C[K] extends AsyncFunc ? K : never]: C[K]
|
||||
}
|
||||
>
|
||||
|
||||
export type Inputs<C extends Record<string, AsyncFunc>> = Simplify<{
|
||||
[T in Operation<C>]: Parameters<C[Cast<T, keyof C>]>[0]
|
||||
}>
|
||||
|
||||
export type Outputs<C extends Record<string, AsyncFunc>> = Simplify<{
|
||||
[T in Operation<C>]: Awaited<ReturnType<C[Cast<T, keyof C>]>>
|
||||
}>
|
||||
|
||||
export type ListOperation<C extends Record<string, AsyncFunc>> = Simplify<
|
||||
keyof {
|
||||
[K in keyof Inputs<C> as Inputs<C>[K] extends { nextToken?: string | undefined } ? K : never]: null
|
||||
}
|
||||
>
|
||||
|
||||
export type ListInputs<C extends Record<string, AsyncFunc>> = Simplify<{
|
||||
[T in ListOperation<C>]: Omit<Inputs<C>[Cast<T, keyof Inputs<C>>], 'nextToken'>
|
||||
}>
|
||||
@@ -0,0 +1,15 @@
|
||||
import { AxiosError } from 'axios'
|
||||
import { UpsertFileResponse } from './gen/public/operations/upsertFile'
|
||||
|
||||
export * from './gen/public/errors'
|
||||
|
||||
export class UploadFileError extends Error {
|
||||
public constructor(
|
||||
message: string,
|
||||
public readonly innerError?: AxiosError,
|
||||
public readonly file?: UpsertFileResponse['file']
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'FileUploadError'
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import axiosRetry from 'axios-retry'
|
||||
import * as common from '../common'
|
||||
import * as gen from '../gen/files'
|
||||
import * as types from '../types'
|
||||
import * as uploadFile from './upload-file'
|
||||
|
||||
type IClient = common.types.Simplify<
|
||||
gen.Client & {
|
||||
uploadFile: (input: uploadFile.UploadFileInput) => Promise<uploadFile.UploadFileOutput>
|
||||
}
|
||||
>
|
||||
|
||||
export type Operation = common.types.Operation<IClient>
|
||||
export type ClientInputs = common.types.Inputs<IClient>
|
||||
export type ClientOutputs = common.types.Outputs<IClient>
|
||||
|
||||
export type ClientProps = common.types.CommonClientProps & {
|
||||
token: string
|
||||
botId: string
|
||||
integrationId?: string
|
||||
integrationAlias?: string
|
||||
}
|
||||
|
||||
export class Client extends gen.Client implements IClient {
|
||||
public readonly config: Readonly<types.ClientConfig>
|
||||
|
||||
public constructor(clientProps: ClientProps) {
|
||||
const clientConfig = common.config.getClientConfig(clientProps)
|
||||
const axiosInstance = common.axios.createAxiosInstance(clientConfig)
|
||||
|
||||
super(axiosInstance, {
|
||||
toApiError: common.errors.toApiError,
|
||||
})
|
||||
|
||||
if (clientProps.retry) {
|
||||
axiosRetry(axiosInstance, clientProps.retry)
|
||||
}
|
||||
|
||||
this.config = clientConfig
|
||||
}
|
||||
|
||||
public get list() {
|
||||
type ListInputs = common.types.ListInputs<IClient>
|
||||
return {
|
||||
files: (props: ListInputs['listFiles']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listFiles({ nextToken, ...props }).then((r) => ({ ...r, items: r.files }))
|
||||
),
|
||||
filePassages: (props: ListInputs['listFilePassages']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listFilePassages({ nextToken, ...props }).then((r) => ({ ...r, items: r.passages }))
|
||||
),
|
||||
fileTags: (props: ListInputs['listFileTags']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listFileTags({ nextToken, ...props }).then((r) => ({ ...r, items: r.tags }))
|
||||
),
|
||||
fileTagValues: (props: ListInputs['listFileTagValues']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listFileTagValues({ nextToken, ...props }).then((r) => ({ ...r, items: r.values }))
|
||||
),
|
||||
knowledgeBases: (props: ListInputs['listKnowledgeBases']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listKnowledgeBases({ nextToken, ...props }).then((r) => ({ ...r, items: r.knowledgeBases }))
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create/update and upload a file in a single step. Returns an object containing the file metadata and the URL to retrieve the file.
|
||||
*/
|
||||
public async uploadFile(input: uploadFile.UploadFileInput): Promise<uploadFile.UploadFileOutput> {
|
||||
return await uploadFile.upload(this, input)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import axios, { AxiosError } from 'axios'
|
||||
import * as common from '../common'
|
||||
import * as errors from '../errors'
|
||||
import { UpsertFileInput, UpsertFileResponse } from '../gen/files/operations/upsertFile'
|
||||
|
||||
export type UploadFileInput = common.types.Simplify<
|
||||
Omit<UpsertFileInput, 'size'> & {
|
||||
content?: ArrayBuffer | Buffer | Blob | Uint8Array | string
|
||||
url?: string
|
||||
}
|
||||
>
|
||||
|
||||
export type UploadFileOutput = UpsertFileResponse
|
||||
|
||||
type UploadFileClient = {
|
||||
upsertFile: (input: UpsertFileInput) => Promise<UpsertFileResponse>
|
||||
}
|
||||
|
||||
export const upload = async (
|
||||
client: UploadFileClient,
|
||||
{
|
||||
key,
|
||||
index,
|
||||
tags,
|
||||
contentType,
|
||||
accessPolicies,
|
||||
content,
|
||||
url,
|
||||
indexing,
|
||||
expiresAt,
|
||||
metadata,
|
||||
publicContentImmediatelyAccessible,
|
||||
}: UploadFileInput
|
||||
): Promise<UploadFileOutput> => {
|
||||
if (url && content) {
|
||||
throw new errors.UploadFileError('Cannot provide both content and URL, please provide only one of them')
|
||||
}
|
||||
|
||||
if (url) {
|
||||
content = await axios
|
||||
.get(url, { responseType: 'arraybuffer' })
|
||||
.then((res) => res.data)
|
||||
.catch((err) => {
|
||||
throw new errors.UploadFileError(`Failed to download file from provided URL: ${err.message}`, err)
|
||||
})
|
||||
}
|
||||
|
||||
if (!content) {
|
||||
throw new errors.UploadFileError('No content was provided for the file')
|
||||
}
|
||||
|
||||
let buffer: ArrayBuffer | Buffer | Blob | Uint8Array
|
||||
let size: number
|
||||
|
||||
if (typeof content === 'string') {
|
||||
const encoder = new TextEncoder()
|
||||
const uint8Array = encoder.encode(content)
|
||||
// Uint8Array is supported by both Node.js and browsers. Buffer.from() is easier but Buffer is only available in Node.js.
|
||||
buffer = uint8Array
|
||||
size = uint8Array.byteLength
|
||||
} else if (content instanceof Uint8Array) {
|
||||
// This supports Buffer too as it's a subclass of Uint8Array
|
||||
buffer = content
|
||||
size = buffer.byteLength
|
||||
} else if (content instanceof ArrayBuffer) {
|
||||
buffer = content
|
||||
size = buffer.byteLength
|
||||
} else if (content instanceof Blob) {
|
||||
buffer = content
|
||||
size = content.size
|
||||
} else {
|
||||
throw new errors.UploadFileError('The provided content is not supported')
|
||||
}
|
||||
|
||||
const { file } = await client.upsertFile({
|
||||
key,
|
||||
tags,
|
||||
index,
|
||||
accessPolicies,
|
||||
contentType,
|
||||
metadata,
|
||||
size,
|
||||
expiresAt,
|
||||
indexing,
|
||||
publicContentImmediatelyAccessible,
|
||||
})
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': file.contentType,
|
||||
}
|
||||
|
||||
if (publicContentImmediatelyAccessible) {
|
||||
headers['x-amz-tagging'] = 'public=true'
|
||||
}
|
||||
|
||||
try {
|
||||
await axios.put(file.uploadUrl, buffer, {
|
||||
maxBodyLength: Infinity,
|
||||
headers,
|
||||
})
|
||||
} catch (thrown: unknown) {
|
||||
const err = thrown instanceof Error ? thrown : new Error(String(thrown))
|
||||
throw new errors.UploadFileError(`Failed to upload file: ${err.message}`, err as AxiosError, file)
|
||||
}
|
||||
|
||||
return {
|
||||
file: {
|
||||
...file,
|
||||
size,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
export * as axios from 'axios'
|
||||
export * as axiosRetry from 'axios-retry'
|
||||
export * as runtime from './runtime'
|
||||
export * as admin from './admin'
|
||||
export * as billing from './billing'
|
||||
export * as files from './files'
|
||||
export * as tables from './tables'
|
||||
export * from './public'
|
||||
export * from './errors'
|
||||
export * from './types'
|
||||
export * from './gen/public/models'
|
||||
@@ -0,0 +1,154 @@
|
||||
import axiosRetry from 'axios-retry'
|
||||
import * as common from '../common'
|
||||
import * as uploadFile from '../files/upload-file'
|
||||
import * as gen from '../gen/public'
|
||||
import * as types from '../types'
|
||||
|
||||
type IClient = common.types.Simplify<
|
||||
gen.Client & {
|
||||
uploadFile: (input: uploadFile.UploadFileInput) => Promise<uploadFile.UploadFileOutput>
|
||||
}
|
||||
>
|
||||
export type Operation = common.types.Operation<IClient>
|
||||
export type ClientInputs = common.types.Inputs<IClient>
|
||||
export type ClientOutputs = common.types.Outputs<IClient>
|
||||
|
||||
export type ClientProps = common.types.CommonClientProps & {
|
||||
integrationId?: string
|
||||
integrationAlias?: string
|
||||
workspaceId?: string
|
||||
botId?: string
|
||||
token?: string
|
||||
}
|
||||
|
||||
export class Client extends gen.Client implements IClient {
|
||||
public readonly config: Readonly<types.ClientConfig>
|
||||
|
||||
public constructor(clientProps: ClientProps = {}) {
|
||||
const clientConfig = common.config.getClientConfig(clientProps)
|
||||
const axiosInstance = common.axios.createAxiosInstance(clientConfig)
|
||||
|
||||
super(axiosInstance, {
|
||||
toApiError: common.errors.toApiError,
|
||||
})
|
||||
|
||||
if (clientProps.retry) {
|
||||
axiosRetry(axiosInstance, clientProps.retry)
|
||||
}
|
||||
|
||||
this.config = clientConfig
|
||||
}
|
||||
|
||||
public get list() {
|
||||
type ListInputs = common.types.ListInputs<IClient>
|
||||
return {
|
||||
conversations: (props: ListInputs['listConversations']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listConversations({ nextToken, ...props }).then((r) => ({ ...r, items: r.conversations }))
|
||||
),
|
||||
participants: (props: ListInputs['listParticipants']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listParticipants({ nextToken, ...props }).then((r) => ({ ...r, items: r.participants }))
|
||||
),
|
||||
events: (props: ListInputs['listEvents']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listEvents({ nextToken, ...props }).then((r) => ({ ...r, items: r.events }))
|
||||
),
|
||||
messages: (props: ListInputs['listMessages']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listMessages({ nextToken, ...props }).then((r) => ({ ...r, items: r.messages }))
|
||||
),
|
||||
users: (props: ListInputs['listUsers']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listUsers({ nextToken, ...props }).then((r) => ({ ...r, items: r.users }))
|
||||
),
|
||||
publicIntegrations: (props: ListInputs['listPublicIntegrations']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listPublicIntegrations({ nextToken, ...props }).then((r) => ({ ...r, items: r.integrations }))
|
||||
),
|
||||
bots: (props: ListInputs['listBots']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listBots({ nextToken, ...props }).then((r) => ({ ...r, items: r.bots }))
|
||||
),
|
||||
botIssues: (props: ListInputs['listBotIssues']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listBotIssues({ nextToken, ...props }).then((r) => ({ ...r, items: r.issues }))
|
||||
),
|
||||
workspaces: (props: ListInputs['listWorkspaces']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listWorkspaces({ nextToken, ...props }).then((r) => ({ ...r, items: r.workspaces }))
|
||||
),
|
||||
publicWorkspaces: (props: ListInputs['listPublicWorkspaces']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listPublicWorkspaces({ nextToken, ...props }).then((r) => ({ ...r, items: r.workspaces }))
|
||||
),
|
||||
workspaceMembers: (props: ListInputs['listWorkspaceMembers']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listWorkspaceMembers({ nextToken, ...props }).then((r) => ({ ...r, items: r.members }))
|
||||
),
|
||||
integrations: (props: ListInputs['listIntegrations']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listIntegrations({ nextToken, ...props }).then((r) => ({ ...r, items: r.integrations }))
|
||||
),
|
||||
interfaces: (props: ListInputs['listInterfaces']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listInterfaces({ nextToken, ...props }).then((r) => ({ ...r, items: r.interfaces }))
|
||||
),
|
||||
publicInterfaces: (props: ListInputs['listPublicInterfaces']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listPublicInterfaces({ nextToken, ...props }).then((r) => ({ ...r, items: r.interfaces }))
|
||||
),
|
||||
plugins: (props: ListInputs['listPlugins']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listPlugins({ nextToken, ...props }).then((r) => ({ ...r, items: r.plugins }))
|
||||
),
|
||||
publicPlugins: (props: ListInputs['listPublicPlugins']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listPublicPlugins({ nextToken, ...props }).then((r) => ({ ...r, items: r.plugins }))
|
||||
),
|
||||
activities: (props: ListInputs['listActivities']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listActivities({ nextToken, ...props }).then((r) => ({ ...r, items: r.activities }))
|
||||
),
|
||||
files: (props: ListInputs['listFiles']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listFiles({ nextToken, ...props }).then((r) => ({ ...r, items: r.files }))
|
||||
),
|
||||
filePassages: (props: ListInputs['listFilePassages']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listFilePassages({ nextToken, ...props }).then((r) => ({ ...r, items: r.passages }))
|
||||
),
|
||||
fileTags: (props: ListInputs['listFileTags']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listFileTags({ nextToken, ...props }).then((r) => ({ ...r, items: r.tags }))
|
||||
),
|
||||
fileTagValues: (props: ListInputs['listFileTagValues']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listFileTagValues({ nextToken, ...props }).then((r) => ({ ...r, items: r.values }))
|
||||
),
|
||||
knowledgeBases: (props: ListInputs['listKnowledgeBases']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listKnowledgeBases({ nextToken, ...props }).then((r) => ({ ...r, items: r.knowledgeBases }))
|
||||
),
|
||||
usageActivity: (props: ListInputs['listUsageActivity']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listUsageActivity({ nextToken, ...props }).then((r) => ({ ...r, items: r.data }))
|
||||
),
|
||||
usageActivityDaily: (props: ListInputs['listUsageActivityDaily']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listUsageActivityDaily({ nextToken, ...props }).then((r) => ({ ...r, items: r.data }))
|
||||
),
|
||||
workflows: (props: ListInputs['listWorkflows']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listWorkflows({ nextToken, ...props }).then((r) => ({ ...r, items: r.workflows }))
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create/update and upload a file in a single step. Returns an object containing the file metadata and the URL to retrieve the file.
|
||||
*/
|
||||
public readonly uploadFile = async (input: uploadFile.UploadFileInput): Promise<uploadFile.UploadFileOutput> => {
|
||||
return await uploadFile.upload(this, input)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import axiosRetry from 'axios-retry'
|
||||
import * as common from '../common'
|
||||
import * as gen from '../gen/runtime'
|
||||
import * as types from '../types'
|
||||
|
||||
type IClient = common.types.Simplify<gen.Client>
|
||||
export type Operation = common.types.Operation<IClient>
|
||||
export type ClientInputs = common.types.Inputs<IClient>
|
||||
export type ClientOutputs = common.types.Outputs<IClient>
|
||||
|
||||
export type ClientProps = common.types.CommonClientProps & {
|
||||
token: string
|
||||
botId: string
|
||||
integrationId?: string
|
||||
integrationAlias?: string
|
||||
}
|
||||
|
||||
export class Client extends gen.Client {
|
||||
public readonly config: Readonly<types.ClientConfig>
|
||||
|
||||
public constructor(clientProps: ClientProps) {
|
||||
const clientConfig = common.config.getClientConfig(clientProps)
|
||||
const axiosInstance = common.axios.createAxiosInstance(clientConfig)
|
||||
|
||||
super(axiosInstance, {
|
||||
toApiError: common.errors.toApiError,
|
||||
})
|
||||
|
||||
if (clientProps.retry) {
|
||||
axiosRetry(axiosInstance, clientProps.retry)
|
||||
}
|
||||
|
||||
this.config = clientConfig
|
||||
}
|
||||
|
||||
public get list() {
|
||||
type ListInputs = common.types.ListInputs<IClient>
|
||||
return {
|
||||
conversations: (props: ListInputs['listConversations']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listConversations({ nextToken, ...props }).then((r) => ({ ...r, items: r.conversations }))
|
||||
),
|
||||
participants: (props: ListInputs['listParticipants']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listParticipants({ nextToken, ...props }).then((r) => ({ ...r, items: r.participants }))
|
||||
),
|
||||
events: (props: ListInputs['listEvents']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listEvents({ nextToken, ...props }).then((r) => ({ ...r, items: r.events }))
|
||||
),
|
||||
messages: (props: ListInputs['listMessages']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listMessages({ nextToken, ...props }).then((r) => ({ ...r, items: r.messages }))
|
||||
),
|
||||
users: (props: ListInputs['listUsers']) =>
|
||||
new common.listing.AsyncCollection(({ nextToken }) =>
|
||||
this.listUsers({ nextToken, ...props }).then((r) => ({ ...r, items: r.users }))
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import axiosRetry from 'axios-retry'
|
||||
import * as common from '../common'
|
||||
import * as gen from '../gen/tables'
|
||||
import * as types from '../types'
|
||||
|
||||
type IClient = common.types.Simplify<gen.Client>
|
||||
export type Operation = common.types.Operation<IClient>
|
||||
export type ClientInputs = common.types.Inputs<IClient>
|
||||
export type ClientOutputs = common.types.Outputs<IClient>
|
||||
|
||||
export type ClientProps = common.types.CommonClientProps & {
|
||||
token: string
|
||||
botId: string
|
||||
integrationId?: string
|
||||
integrationAlias?: string
|
||||
}
|
||||
|
||||
export class Client extends gen.Client {
|
||||
public readonly config: Readonly<types.ClientConfig>
|
||||
|
||||
public constructor(clientProps: ClientProps) {
|
||||
const clientConfig = common.config.getClientConfig(clientProps)
|
||||
const axiosInstance = common.axios.createAxiosInstance(clientConfig)
|
||||
|
||||
super(axiosInstance, {
|
||||
toApiError: common.errors.toApiError,
|
||||
})
|
||||
|
||||
if (clientProps.retry) {
|
||||
axiosRetry(axiosInstance, clientProps.retry)
|
||||
}
|
||||
|
||||
this.config = clientConfig
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IAxiosRetryConfig } from 'axios-retry'
|
||||
|
||||
export type Headers = Record<string, string | string[]>
|
||||
|
||||
export type RetryConfig = IAxiosRetryConfig
|
||||
|
||||
export type ClientConfig = {
|
||||
apiUrl: string
|
||||
headers: Headers
|
||||
withCredentials: boolean
|
||||
timeout: number
|
||||
debug: boolean
|
||||
}
|
||||
Reference in New Issue
Block a user