From 751cfec21f9c88b3155bda870feef0d80672749d Mon Sep 17 00:00:00 2001 From: stainless-bot Date: Wed, 8 May 2024 09:34:24 +0000 Subject: [PATCH] feat(api): update via SDK Studio --- .stats.yml | 2 +- README.md | 39 +++----------- api.md | 14 ++--- src/index.ts | 27 +++++++++- src/resources/apps/apps.ts | 1 - src/resources/apps/index.ts | 10 +--- .../apps/users/collections/collections.ts | 40 +++++++------- .../apps/users/collections/documents.ts | 39 ++++++++------ src/resources/apps/users/collections/index.ts | 2 - src/resources/apps/users/index.ts | 12 +---- src/resources/apps/users/sessions/index.ts | 3 -- src/resources/apps/users/sessions/messages.ts | 39 ++++++++------ .../apps/users/sessions/metamessages.ts | 39 ++++++++------ src/resources/apps/users/sessions/sessions.ts | 43 ++++++++------- src/resources/apps/users/users.ts | 38 ++++++------- tests/api-resources/apps/apps.test.ts | 5 +- .../users/collections/collections.test.ts | 5 +- .../apps/users/collections/documents.test.ts | 5 +- .../apps/users/sessions/messages.test.ts | 5 +- .../apps/users/sessions/metamessages.test.ts | 5 +- .../apps/users/sessions/sessions.test.ts | 5 +- tests/api-resources/apps/users/users.test.ts | 5 +- tests/index.test.ts | 54 ++++++++++++++----- 23 files changed, 235 insertions(+), 202 deletions(-) diff --git a/.stats.yml b/.stats.yml index 0aecd86..32ba19b 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ configured_endpoints: 38 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/plastic-labs%2Fhoncho-72865b06002229b1e9c394214250259bfa3eb0053af2ab20aabe02940b6e40a8.yml +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/plastic-labs%2Fhoncho-60707ccc3985eec6cb8cc1ba1bdd9eefb50e37aeeb524e34ad05dcfebcb12a77.yml diff --git a/README.md b/README.md index 614f4d5..62b4218 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,9 @@ The full API of this library can be found in [api.md](api.md). ```js import Honcho from 'honcho'; -const honcho = new Honcho(); +const honcho = new Honcho({ + apiKey: process.env['HONCHO_AUTH_TOKEN'], // This is the default and can be omitted +}); async function main() { const app = await honcho.apps.create({ name: 'string' }); @@ -44,7 +46,9 @@ This library includes TypeScript definitions for all request params and response ```ts import Honcho from 'honcho'; -const honcho = new Honcho(); +const honcho = new Honcho({ + apiKey: process.env['HONCHO_AUTH_TOKEN'], // This is the default and can be omitted +}); async function main() { const params: Honcho.AppCreateParams = { name: 'string' }; @@ -134,37 +138,6 @@ On timeout, an `APIConnectionTimeoutError` is thrown. Note that requests which time out will be [retried twice by default](#retries). -## Auto-pagination - -List methods in the Honcho API are paginated. -You can use `for await … of` syntax to iterate through items across all pages: - -```ts -async function fetchAllAppsUsers(params) { - const allAppsUsers = []; - // Automatically fetches more pages as needed. - for await (const user of honcho.apps.users.list('REPLACE_ME')) { - allAppsUsers.push(user); - } - return allAppsUsers; -} -``` - -Alternatively, you can make request a single page at a time: - -```ts -let page = await honcho.apps.users.list('REPLACE_ME'); -for (const user of page.items) { - console.log(user); -} - -// Convenience methods are provided for manually paginating: -while (page.hasNextPage()) { - page = page.getNextPage(); - // ... -} -``` - ## Advanced Usage ### Accessing raw Response data (e.g., headers) diff --git a/api.md b/api.md index f164324..7315aaa 100644 --- a/api.md +++ b/api.md @@ -23,7 +23,7 @@ Methods: - client.apps.users.create(appId, { ...params }) -> User - client.apps.users.update(appId, userId, { ...params }) -> User -- client.apps.users.list(appId, { ...params }) -> UsersPage +- client.apps.users.list(appId, { ...params }) -> PageUser - client.apps.users.get(appId, userId) -> User - client.apps.users.getByName(appId, name) -> User - client.apps.users.getOrCreate(appId, name) -> User @@ -42,11 +42,11 @@ Methods: - client.apps.users.sessions.create(appId, userId, { ...params }) -> Session - client.apps.users.sessions.update(appId, userId, sessionId, { ...params }) -> Session -- client.apps.users.sessions.list(appId, userId, { ...params }) -> SessionsPage +- client.apps.users.sessions.list(appId, userId, { ...params }) -> PageSession - client.apps.users.sessions.delete(appId, userId, sessionId) -> unknown - client.apps.users.sessions.chat(appId, userId, sessionId, { ...params }) -> AgentChat - client.apps.users.sessions.get(appId, userId, sessionId) -> Session -- client.apps.users.sessions.stream(appId, userId, sessionId, { ...params }) -> string +- client.apps.users.sessions.stream(appId, userId, sessionId, { ...params }) -> unknown #### Messages @@ -59,7 +59,7 @@ Methods: - client.apps.users.sessions.messages.create(appId, userId, sessionId, { ...params }) -> Message - client.apps.users.sessions.messages.update(appId, userId, sessionId, messageId, { ...params }) -> Message -- client.apps.users.sessions.messages.list(appId, userId, sessionId, { ...params }) -> MessagesPage +- client.apps.users.sessions.messages.list(appId, userId, sessionId, { ...params }) -> PageMessage - client.apps.users.sessions.messages.get(appId, userId, sessionId, messageId) -> Message #### Metamessages @@ -73,7 +73,7 @@ Methods: - client.apps.users.sessions.metamessages.create(appId, userId, sessionId, { ...params }) -> Metamessage - client.apps.users.sessions.metamessages.update(appId, userId, sessionId, metamessageId, { ...params }) -> Metamessage -- client.apps.users.sessions.metamessages.list(appId, userId, sessionId, { ...params }) -> MetamessagesPage +- client.apps.users.sessions.metamessages.list(appId, userId, sessionId, { ...params }) -> PageMetamessage - client.apps.users.sessions.metamessages.get(appId, userId, sessionId, metamessageId, { ...params }) -> Metamessage ### Collections @@ -89,7 +89,7 @@ Methods: - client.apps.users.collections.create(appId, userId, { ...params }) -> Collection - client.apps.users.collections.update(appId, userId, collectionId, { ...params }) -> Collection -- client.apps.users.collections.list(appId, userId, { ...params }) -> CollectionsPage +- client.apps.users.collections.list(appId, userId, { ...params }) -> PageCollection - client.apps.users.collections.delete(appId, userId, collectionId) -> unknown - client.apps.users.collections.get(appId, userId, collectionId) -> Collection - client.apps.users.collections.getByName(appId, userId, name) -> Collection @@ -107,6 +107,6 @@ Methods: - client.apps.users.collections.documents.create(appId, userId, collectionId, { ...params }) -> Document - client.apps.users.collections.documents.update(appId, userId, collectionId, documentId, { ...params }) -> Document -- client.apps.users.collections.documents.list(appId, userId, collectionId, { ...params }) -> DocumentsPage +- client.apps.users.collections.documents.list(appId, userId, collectionId, { ...params }) -> PageDocument - client.apps.users.collections.documents.delete(appId, userId, collectionId, documentId) -> unknown - client.apps.users.collections.documents.get(appId, userId, collectionId, documentId) -> Document diff --git a/src/index.ts b/src/index.ts index 8b15337..8688a3e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,11 @@ import * as Pagination from 'honcho/pagination'; import * as API from 'honcho/resources/index'; export interface ClientOptions { + /** + * Defaults to process.env['HONCHO_AUTH_TOKEN']. + */ + apiKey?: string | undefined; + /** * Override the default base URL for the API, e.g., "https://api.example.com/v2/" * @@ -67,11 +72,14 @@ export interface ClientOptions { /** API Client for interfacing with the Honcho API. */ export class Honcho extends Core.APIClient { + apiKey: string; + private _options: ClientOptions; /** * API Client for interfacing with the Honcho API. * + * @param {string | undefined} [opts.apiKey=process.env['HONCHO_AUTH_TOKEN'] ?? undefined] * @param {string} [opts.baseURL=process.env['HONCHO_BASE_URL'] ?? https://localhost:8080/test-api] - Override the default base URL for the API. * @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out. * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections. @@ -80,8 +88,19 @@ export class Honcho extends Core.APIClient { * @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API. * @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API. */ - constructor({ baseURL = Core.readEnv('HONCHO_BASE_URL'), ...opts }: ClientOptions = {}) { + constructor({ + baseURL = Core.readEnv('HONCHO_BASE_URL'), + apiKey = Core.readEnv('HONCHO_AUTH_TOKEN'), + ...opts + }: ClientOptions = {}) { + if (apiKey === undefined) { + throw new Errors.HonchoError( + "The HONCHO_AUTH_TOKEN environment variable is missing or empty; either provide it, or instantiate the Honcho client with an apiKey option, like new Honcho({ apiKey: 'My API Key' }).", + ); + } + const options: ClientOptions = { + apiKey, ...opts, baseURL: baseURL || `https://localhost:8080/test-api`, }; @@ -94,6 +113,8 @@ export class Honcho extends Core.APIClient { fetch: options.fetch, }); this._options = options; + + this.apiKey = apiKey; } apps: API.Apps = new API.Apps(this); @@ -109,6 +130,10 @@ export class Honcho extends Core.APIClient { }; } + protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers { + return { Authorization: `Bearer ${this.apiKey}` }; + } + static Honcho = this; static HonchoError = Errors.HonchoError; diff --git a/src/resources/apps/apps.ts b/src/resources/apps/apps.ts index 23623da..90b2652 100644 --- a/src/resources/apps/apps.ts +++ b/src/resources/apps/apps.ts @@ -94,7 +94,6 @@ export namespace Apps { export import Users = UsersAPI.Users; export import PageUser = UsersAPI.PageUser; export import User = UsersAPI.User; - export import UsersPage = UsersAPI.UsersPage; export import UserCreateParams = UsersAPI.UserCreateParams; export import UserUpdateParams = UsersAPI.UserUpdateParams; export import UserListParams = UsersAPI.UserListParams; diff --git a/src/resources/apps/index.ts b/src/resources/apps/index.ts index e1a9145..507b4b9 100644 --- a/src/resources/apps/index.ts +++ b/src/resources/apps/index.ts @@ -1,12 +1,4 @@ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. export { App, AppCreateParams, AppUpdateParams, Apps } from './apps'; -export { - PageUser, - User, - UserCreateParams, - UserUpdateParams, - UserListParams, - UsersPage, - Users, -} from './users/index'; +export { PageUser, User, UserCreateParams, UserUpdateParams, UserListParams, Users } from './users/index'; diff --git a/src/resources/apps/users/collections/collections.ts b/src/resources/apps/users/collections/collections.ts index 6f43779..57484ee 100644 --- a/src/resources/apps/users/collections/collections.ts +++ b/src/resources/apps/users/collections/collections.ts @@ -5,7 +5,6 @@ import { APIResource } from 'honcho/resource'; import { isRequestOptions } from 'honcho/core'; import * as CollectionsAPI from 'honcho/resources/apps/users/collections/collections'; import * as DocumentsAPI from 'honcho/resources/apps/users/collections/documents'; -import { Page, type PageParams } from 'honcho/pagination'; export class Collections extends APIResource { documents: DocumentsAPI.Documents = new DocumentsAPI.Documents(this._client); @@ -52,25 +51,18 @@ export class Collections extends APIResource { userId: string, query?: CollectionListParams, options?: Core.RequestOptions, - ): Core.PagePromise; - list( - appId: string, - userId: string, - options?: Core.RequestOptions, - ): Core.PagePromise; + ): Core.APIPromise; + list(appId: string, userId: string, options?: Core.RequestOptions): Core.APIPromise; list( appId: string, userId: string, query: CollectionListParams | Core.RequestOptions = {}, options?: Core.RequestOptions, - ): Core.PagePromise { + ): Core.APIPromise { if (isRequestOptions(query)) { return this.list(appId, userId, {}, query); } - return this._client.getAPIList(`/apps/${appId}/users/${userId}/collections`, CollectionsPage, { - query, - ...options, - }); + return this._client.get(`/apps/${appId}/users/${userId}/collections`, { query, ...options }); } /** @@ -126,8 +118,6 @@ export class Collections extends APIResource { } } -export class CollectionsPage extends Page {} - export interface Collection { id: string; @@ -143,13 +133,13 @@ export interface Collection { export interface PageCollection { items: Array; - page: number; + page: number | null; - size: number; + size: number | null; - total: number; + total: number | null; - pages?: number; + pages?: number | null; } export type CollectionDeleteResponse = unknown; @@ -168,10 +158,20 @@ export interface CollectionUpdateParams { metadata?: unknown | null; } -export interface CollectionListParams extends PageParams { +export interface CollectionListParams { filter?: string | null; + /** + * Page number + */ + page?: number; + reverse?: boolean | null; + + /** + * Page size + */ + size?: number; } export interface CollectionQueryParams { @@ -187,7 +187,6 @@ export namespace Collections { export import PageCollection = CollectionsAPI.PageCollection; export import CollectionDeleteResponse = CollectionsAPI.CollectionDeleteResponse; export import CollectionQueryResponse = CollectionsAPI.CollectionQueryResponse; - export import CollectionsPage = CollectionsAPI.CollectionsPage; export import CollectionCreateParams = CollectionsAPI.CollectionCreateParams; export import CollectionUpdateParams = CollectionsAPI.CollectionUpdateParams; export import CollectionListParams = CollectionsAPI.CollectionListParams; @@ -196,7 +195,6 @@ export namespace Collections { export import Document = DocumentsAPI.Document; export import PageDocument = DocumentsAPI.PageDocument; export import DocumentDeleteResponse = DocumentsAPI.DocumentDeleteResponse; - export import DocumentsPage = DocumentsAPI.DocumentsPage; export import DocumentCreateParams = DocumentsAPI.DocumentCreateParams; export import DocumentUpdateParams = DocumentsAPI.DocumentUpdateParams; export import DocumentListParams = DocumentsAPI.DocumentListParams; diff --git a/src/resources/apps/users/collections/documents.ts b/src/resources/apps/users/collections/documents.ts index 876bad0..99d5322 100644 --- a/src/resources/apps/users/collections/documents.ts +++ b/src/resources/apps/users/collections/documents.ts @@ -4,7 +4,6 @@ import * as Core from 'honcho/core'; import { APIResource } from 'honcho/resource'; import { isRequestOptions } from 'honcho/core'; import * as DocumentsAPI from 'honcho/resources/apps/users/collections/documents'; -import { Page, type PageParams } from 'honcho/pagination'; export class Documents extends APIResource { /** @@ -49,28 +48,27 @@ export class Documents extends APIResource { collectionId: string, query?: DocumentListParams, options?: Core.RequestOptions, - ): Core.PagePromise; + ): Core.APIPromise; list( appId: string, userId: string, collectionId: string, options?: Core.RequestOptions, - ): Core.PagePromise; + ): Core.APIPromise; list( appId: string, userId: string, collectionId: string, query: DocumentListParams | Core.RequestOptions = {}, options?: Core.RequestOptions, - ): Core.PagePromise { + ): Core.APIPromise { if (isRequestOptions(query)) { return this.list(appId, userId, collectionId, {}, query); } - return this._client.getAPIList( - `/apps/${appId}/users/${userId}/collections/${collectionId}/documents`, - DocumentsPage, - { query, ...options }, - ); + return this._client.get(`/apps/${appId}/users/${userId}/collections/${collectionId}/documents`, { + query, + ...options, + }); } /** @@ -106,8 +104,6 @@ export class Documents extends APIResource { } } -export class DocumentsPage extends Page {} - export interface Document { id: string; @@ -123,13 +119,13 @@ export interface Document { export interface PageDocument { items: Array; - page: number; + page: number | null; - size: number; + size: number | null; - total: number; + total: number | null; - pages?: number; + pages?: number | null; } export type DocumentDeleteResponse = unknown; @@ -146,17 +142,26 @@ export interface DocumentUpdateParams { metadata?: unknown | null; } -export interface DocumentListParams extends PageParams { +export interface DocumentListParams { filter?: string | null; + /** + * Page number + */ + page?: number; + reverse?: boolean | null; + + /** + * Page size + */ + size?: number; } export namespace Documents { export import Document = DocumentsAPI.Document; export import PageDocument = DocumentsAPI.PageDocument; export import DocumentDeleteResponse = DocumentsAPI.DocumentDeleteResponse; - export import DocumentsPage = DocumentsAPI.DocumentsPage; export import DocumentCreateParams = DocumentsAPI.DocumentCreateParams; export import DocumentUpdateParams = DocumentsAPI.DocumentUpdateParams; export import DocumentListParams = DocumentsAPI.DocumentListParams; diff --git a/src/resources/apps/users/collections/index.ts b/src/resources/apps/users/collections/index.ts index b8db4f6..1d9932c 100644 --- a/src/resources/apps/users/collections/index.ts +++ b/src/resources/apps/users/collections/index.ts @@ -9,7 +9,6 @@ export { CollectionUpdateParams, CollectionListParams, CollectionQueryParams, - CollectionsPage, Collections, } from './collections'; export { @@ -19,6 +18,5 @@ export { DocumentCreateParams, DocumentUpdateParams, DocumentListParams, - DocumentsPage, Documents, } from './documents'; diff --git a/src/resources/apps/users/index.ts b/src/resources/apps/users/index.ts index 63b66a7..e3252ab 100644 --- a/src/resources/apps/users/index.ts +++ b/src/resources/apps/users/index.ts @@ -11,7 +11,6 @@ export { SessionListParams, SessionChatParams, SessionStreamParams, - SessionsPage, Sessions, } from './sessions/index'; export { @@ -23,15 +22,6 @@ export { CollectionUpdateParams, CollectionListParams, CollectionQueryParams, - CollectionsPage, Collections, } from './collections/index'; -export { - PageUser, - User, - UserCreateParams, - UserUpdateParams, - UserListParams, - UsersPage, - Users, -} from './users'; +export { PageUser, User, UserCreateParams, UserUpdateParams, UserListParams, Users } from './users'; diff --git a/src/resources/apps/users/sessions/index.ts b/src/resources/apps/users/sessions/index.ts index 4580149..27bf702 100644 --- a/src/resources/apps/users/sessions/index.ts +++ b/src/resources/apps/users/sessions/index.ts @@ -11,7 +11,6 @@ export { SessionListParams, SessionChatParams, SessionStreamParams, - SessionsPage, Sessions, } from './sessions'; export { @@ -20,7 +19,6 @@ export { MessageCreateParams, MessageUpdateParams, MessageListParams, - MessagesPage, Messages, } from './messages'; export { @@ -30,6 +28,5 @@ export { MetamessageUpdateParams, MetamessageListParams, MetamessageGetParams, - MetamessagesPage, Metamessages, } from './metamessages'; diff --git a/src/resources/apps/users/sessions/messages.ts b/src/resources/apps/users/sessions/messages.ts index a861de9..9a17c13 100644 --- a/src/resources/apps/users/sessions/messages.ts +++ b/src/resources/apps/users/sessions/messages.ts @@ -4,7 +4,6 @@ import * as Core from 'honcho/core'; import { APIResource } from 'honcho/resource'; import { isRequestOptions } from 'honcho/core'; import * as MessagesAPI from 'honcho/resources/apps/users/sessions/messages'; -import { Page, type PageParams } from 'honcho/pagination'; export class Messages extends APIResource { /** @@ -68,28 +67,27 @@ export class Messages extends APIResource { sessionId: string, query?: MessageListParams, options?: Core.RequestOptions, - ): Core.PagePromise; + ): Core.APIPromise; list( appId: string, userId: string, sessionId: string, options?: Core.RequestOptions, - ): Core.PagePromise; + ): Core.APIPromise; list( appId: string, userId: string, sessionId: string, query: MessageListParams | Core.RequestOptions = {}, options?: Core.RequestOptions, - ): Core.PagePromise { + ): Core.APIPromise { if (isRequestOptions(query)) { return this.list(appId, userId, sessionId, {}, query); } - return this._client.getAPIList( - `/apps/${appId}/users/${userId}/sessions/${sessionId}/messages`, - MessagesPage, - { query, ...options }, - ); + return this._client.get(`/apps/${appId}/users/${userId}/sessions/${sessionId}/messages`, { + query, + ...options, + }); } /** @@ -109,8 +107,6 @@ export class Messages extends APIResource { } } -export class MessagesPage extends Page {} - export interface Message { id: string; @@ -128,13 +124,13 @@ export interface Message { export interface PageMessage { items: Array; - page: number; + page: number | null; - size: number; + size: number | null; - total: number; + total: number | null; - pages?: number; + pages?: number | null; } export interface MessageCreateParams { @@ -149,16 +145,25 @@ export interface MessageUpdateParams { metadata?: unknown | null; } -export interface MessageListParams extends PageParams { +export interface MessageListParams { filter?: string | null; + /** + * Page number + */ + page?: number; + reverse?: boolean | null; + + /** + * Page size + */ + size?: number; } export namespace Messages { export import Message = MessagesAPI.Message; export import PageMessage = MessagesAPI.PageMessage; - export import MessagesPage = MessagesAPI.MessagesPage; export import MessageCreateParams = MessagesAPI.MessageCreateParams; export import MessageUpdateParams = MessagesAPI.MessageUpdateParams; export import MessageListParams = MessagesAPI.MessageListParams; diff --git a/src/resources/apps/users/sessions/metamessages.ts b/src/resources/apps/users/sessions/metamessages.ts index cfd91e4..6d02bc2 100644 --- a/src/resources/apps/users/sessions/metamessages.ts +++ b/src/resources/apps/users/sessions/metamessages.ts @@ -4,7 +4,6 @@ import * as Core from 'honcho/core'; import { APIResource } from 'honcho/resource'; import { isRequestOptions } from 'honcho/core'; import * as MetamessagesAPI from 'honcho/resources/apps/users/sessions/metamessages'; -import { Page, type PageParams } from 'honcho/pagination'; export class Metamessages extends APIResource { /** @@ -68,28 +67,27 @@ export class Metamessages extends APIResource { sessionId: string, query?: MetamessageListParams, options?: Core.RequestOptions, - ): Core.PagePromise; + ): Core.APIPromise; list( appId: string, userId: string, sessionId: string, options?: Core.RequestOptions, - ): Core.PagePromise; + ): Core.APIPromise; list( appId: string, userId: string, sessionId: string, query: MetamessageListParams | Core.RequestOptions = {}, options?: Core.RequestOptions, - ): Core.PagePromise { + ): Core.APIPromise { if (isRequestOptions(query)) { return this.list(appId, userId, sessionId, {}, query); } - return this._client.getAPIList( - `/apps/${appId}/users/${userId}/sessions/${sessionId}/metamessages`, - MetamessagesPage, - { query, ...options }, - ); + return this._client.get(`/apps/${appId}/users/${userId}/sessions/${sessionId}/metamessages`, { + query, + ...options, + }); } /** @@ -118,8 +116,6 @@ export class Metamessages extends APIResource { } } -export class MetamessagesPage extends Page {} - export interface Metamessage { id: string; @@ -137,13 +133,13 @@ export interface Metamessage { export interface PageMetamessage { items: Array; - page: number; + page: number | null; - size: number; + size: number | null; - total: number; + total: number | null; - pages?: number; + pages?: number | null; } export interface MetamessageCreateParams { @@ -164,14 +160,24 @@ export interface MetamessageUpdateParams { metamessage_type?: string | null; } -export interface MetamessageListParams extends PageParams { +export interface MetamessageListParams { filter?: string | null; message_id?: string | null; metamessage_type?: string | null; + /** + * Page number + */ + page?: number; + reverse?: boolean | null; + + /** + * Page size + */ + size?: number; } export interface MetamessageGetParams { @@ -181,7 +187,6 @@ export interface MetamessageGetParams { export namespace Metamessages { export import Metamessage = MetamessagesAPI.Metamessage; export import PageMetamessage = MetamessagesAPI.PageMetamessage; - export import MetamessagesPage = MetamessagesAPI.MetamessagesPage; export import MetamessageCreateParams = MetamessagesAPI.MetamessageCreateParams; export import MetamessageUpdateParams = MetamessagesAPI.MetamessageUpdateParams; export import MetamessageListParams = MetamessagesAPI.MetamessageListParams; diff --git a/src/resources/apps/users/sessions/sessions.ts b/src/resources/apps/users/sessions/sessions.ts index fec37c0..3fa454b 100644 --- a/src/resources/apps/users/sessions/sessions.ts +++ b/src/resources/apps/users/sessions/sessions.ts @@ -6,8 +6,6 @@ import { isRequestOptions } from 'honcho/core'; import * as SessionsAPI from 'honcho/resources/apps/users/sessions/sessions'; import * as MessagesAPI from 'honcho/resources/apps/users/sessions/messages'; import * as MetamessagesAPI from 'honcho/resources/apps/users/sessions/metamessages'; -import { type Uploadable } from 'honcho/core'; -import { Page, type PageParams } from 'honcho/pagination'; export class Sessions extends APIResource { messages: MessagesAPI.Messages = new MessagesAPI.Messages(this._client); @@ -67,21 +65,18 @@ export class Sessions extends APIResource { userId: string, query?: SessionListParams, options?: Core.RequestOptions, - ): Core.PagePromise; - list(appId: string, userId: string, options?: Core.RequestOptions): Core.PagePromise; + ): Core.APIPromise; + list(appId: string, userId: string, options?: Core.RequestOptions): Core.APIPromise; list( appId: string, userId: string, query: SessionListParams | Core.RequestOptions = {}, options?: Core.RequestOptions, - ): Core.PagePromise { + ): Core.APIPromise { if (isRequestOptions(query)) { return this.list(appId, userId, {}, query); } - return this._client.getAPIList(`/apps/${appId}/users/${userId}/sessions`, SessionsPage, { - query, - ...options, - }); + return this._client.get(`/apps/${appId}/users/${userId}/sessions`, { query, ...options }); } /** @@ -149,17 +144,14 @@ export class Sessions extends APIResource { sessionId: string, query: SessionStreamParams, options?: Core.RequestOptions, - ): Core.APIPromise { + ): Core.APIPromise { return this._client.get(`/apps/${appId}/users/${userId}/sessions/${sessionId}/chat/stream`, { query, ...options, - headers: { Accept: 'text/event-stream', ...options?.headers }, }); } } -export class SessionsPage extends Page {} - export interface AgentChat { content: string; } @@ -167,13 +159,13 @@ export interface AgentChat { export interface PageSession { items: Array; - page: number; + page: number | null; - size: number; + size: number | null; - total: number; + total: number | null; - pages?: number; + pages?: number | null; } export interface Session { @@ -192,7 +184,7 @@ export interface Session { export type SessionDeleteResponse = unknown; -export type SessionStreamResponse = Uploadable; +export type SessionStreamResponse = unknown; export interface SessionCreateParams { location_id: string; @@ -204,14 +196,24 @@ export interface SessionUpdateParams { metadata?: unknown | null; } -export interface SessionListParams extends PageParams { +export interface SessionListParams { filter?: string | null; is_active?: boolean | null; location_id?: string | null; + /** + * Page number + */ + page?: number; + reverse?: boolean | null; + + /** + * Page size + */ + size?: number; } export interface SessionChatParams { @@ -228,7 +230,6 @@ export namespace Sessions { export import Session = SessionsAPI.Session; export import SessionDeleteResponse = SessionsAPI.SessionDeleteResponse; export import SessionStreamResponse = SessionsAPI.SessionStreamResponse; - export import SessionsPage = SessionsAPI.SessionsPage; export import SessionCreateParams = SessionsAPI.SessionCreateParams; export import SessionUpdateParams = SessionsAPI.SessionUpdateParams; export import SessionListParams = SessionsAPI.SessionListParams; @@ -237,14 +238,12 @@ export namespace Sessions { export import Messages = MessagesAPI.Messages; export import Message = MessagesAPI.Message; export import PageMessage = MessagesAPI.PageMessage; - export import MessagesPage = MessagesAPI.MessagesPage; export import MessageCreateParams = MessagesAPI.MessageCreateParams; export import MessageUpdateParams = MessagesAPI.MessageUpdateParams; export import MessageListParams = MessagesAPI.MessageListParams; export import Metamessages = MetamessagesAPI.Metamessages; export import Metamessage = MetamessagesAPI.Metamessage; export import PageMetamessage = MetamessagesAPI.PageMetamessage; - export import MetamessagesPage = MetamessagesAPI.MetamessagesPage; export import MetamessageCreateParams = MetamessagesAPI.MetamessageCreateParams; export import MetamessageUpdateParams = MetamessagesAPI.MetamessageUpdateParams; export import MetamessageListParams = MetamessagesAPI.MetamessageListParams; diff --git a/src/resources/apps/users/users.ts b/src/resources/apps/users/users.ts index 54bbc86..58780bc 100644 --- a/src/resources/apps/users/users.ts +++ b/src/resources/apps/users/users.ts @@ -6,7 +6,6 @@ import { isRequestOptions } from 'honcho/core'; import * as UsersAPI from 'honcho/resources/apps/users/users'; import * as CollectionsAPI from 'honcho/resources/apps/users/collections/collections'; import * as SessionsAPI from 'honcho/resources/apps/users/sessions/sessions'; -import { Page, type PageParams } from 'honcho/pagination'; export class Users extends APIResource { sessions: SessionsAPI.Sessions = new SessionsAPI.Sessions(this._client); @@ -50,21 +49,17 @@ export class Users extends APIResource { * * Returns: list[schemas.User]: List of User objects */ - list( - appId: string, - query?: UserListParams, - options?: Core.RequestOptions, - ): Core.PagePromise; - list(appId: string, options?: Core.RequestOptions): Core.PagePromise; + list(appId: string, query?: UserListParams, options?: Core.RequestOptions): Core.APIPromise; + list(appId: string, options?: Core.RequestOptions): Core.APIPromise; list( appId: string, query: UserListParams | Core.RequestOptions = {}, options?: Core.RequestOptions, - ): Core.PagePromise { + ): Core.APIPromise { if (isRequestOptions(query)) { return this.list(appId, {}, query); } - return this._client.getAPIList(`/apps/${appId}/users`, UsersPage, { query, ...options }); + return this._client.get(`/apps/${appId}/users`, { query, ...options }); } /** @@ -107,18 +102,16 @@ export class Users extends APIResource { } } -export class UsersPage extends Page {} - export interface PageUser { items: Array; - page: number; + page: number | null; - size: number; + size: number | null; - total: number; + total: number | null; - pages?: number; + pages?: number | null; } export interface User { @@ -145,16 +138,25 @@ export interface UserUpdateParams { name?: string | null; } -export interface UserListParams extends PageParams { +export interface UserListParams { filter?: string | null; + /** + * Page number + */ + page?: number; + reverse?: boolean; + + /** + * Page size + */ + size?: number; } export namespace Users { export import PageUser = UsersAPI.PageUser; export import User = UsersAPI.User; - export import UsersPage = UsersAPI.UsersPage; export import UserCreateParams = UsersAPI.UserCreateParams; export import UserUpdateParams = UsersAPI.UserUpdateParams; export import UserListParams = UsersAPI.UserListParams; @@ -164,7 +166,6 @@ export namespace Users { export import Session = SessionsAPI.Session; export import SessionDeleteResponse = SessionsAPI.SessionDeleteResponse; export import SessionStreamResponse = SessionsAPI.SessionStreamResponse; - export import SessionsPage = SessionsAPI.SessionsPage; export import SessionCreateParams = SessionsAPI.SessionCreateParams; export import SessionUpdateParams = SessionsAPI.SessionUpdateParams; export import SessionListParams = SessionsAPI.SessionListParams; @@ -175,7 +176,6 @@ export namespace Users { export import PageCollection = CollectionsAPI.PageCollection; export import CollectionDeleteResponse = CollectionsAPI.CollectionDeleteResponse; export import CollectionQueryResponse = CollectionsAPI.CollectionQueryResponse; - export import CollectionsPage = CollectionsAPI.CollectionsPage; export import CollectionCreateParams = CollectionsAPI.CollectionCreateParams; export import CollectionUpdateParams = CollectionsAPI.CollectionUpdateParams; export import CollectionListParams = CollectionsAPI.CollectionListParams; diff --git a/tests/api-resources/apps/apps.test.ts b/tests/api-resources/apps/apps.test.ts index e28ec1c..7b653f3 100644 --- a/tests/api-resources/apps/apps.test.ts +++ b/tests/api-resources/apps/apps.test.ts @@ -3,7 +3,10 @@ import Honcho from 'honcho'; import { Response } from 'node-fetch'; -const honcho = new Honcho({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' }); +const honcho = new Honcho({ + apiKey: 'My API Key', + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', +}); describe('resource apps', () => { test('create: only required params', async () => { diff --git a/tests/api-resources/apps/users/collections/collections.test.ts b/tests/api-resources/apps/users/collections/collections.test.ts index 3ce7ae3..d6ddcc6 100644 --- a/tests/api-resources/apps/users/collections/collections.test.ts +++ b/tests/api-resources/apps/users/collections/collections.test.ts @@ -3,7 +3,10 @@ import Honcho from 'honcho'; import { Response } from 'node-fetch'; -const honcho = new Honcho({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' }); +const honcho = new Honcho({ + apiKey: 'My API Key', + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', +}); describe('resource collections', () => { test('create: only required params', async () => { diff --git a/tests/api-resources/apps/users/collections/documents.test.ts b/tests/api-resources/apps/users/collections/documents.test.ts index f862006..85d49fe 100644 --- a/tests/api-resources/apps/users/collections/documents.test.ts +++ b/tests/api-resources/apps/users/collections/documents.test.ts @@ -3,7 +3,10 @@ import Honcho from 'honcho'; import { Response } from 'node-fetch'; -const honcho = new Honcho({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' }); +const honcho = new Honcho({ + apiKey: 'My API Key', + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', +}); describe('resource documents', () => { test('create: only required params', async () => { diff --git a/tests/api-resources/apps/users/sessions/messages.test.ts b/tests/api-resources/apps/users/sessions/messages.test.ts index 8724f20..e15ff54 100644 --- a/tests/api-resources/apps/users/sessions/messages.test.ts +++ b/tests/api-resources/apps/users/sessions/messages.test.ts @@ -3,7 +3,10 @@ import Honcho from 'honcho'; import { Response } from 'node-fetch'; -const honcho = new Honcho({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' }); +const honcho = new Honcho({ + apiKey: 'My API Key', + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', +}); describe('resource messages', () => { test('create: only required params', async () => { diff --git a/tests/api-resources/apps/users/sessions/metamessages.test.ts b/tests/api-resources/apps/users/sessions/metamessages.test.ts index 2bae3a9..5957a11 100644 --- a/tests/api-resources/apps/users/sessions/metamessages.test.ts +++ b/tests/api-resources/apps/users/sessions/metamessages.test.ts @@ -3,7 +3,10 @@ import Honcho from 'honcho'; import { Response } from 'node-fetch'; -const honcho = new Honcho({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' }); +const honcho = new Honcho({ + apiKey: 'My API Key', + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', +}); describe('resource metamessages', () => { test('create: only required params', async () => { diff --git a/tests/api-resources/apps/users/sessions/sessions.test.ts b/tests/api-resources/apps/users/sessions/sessions.test.ts index f37fda2..04372a4 100644 --- a/tests/api-resources/apps/users/sessions/sessions.test.ts +++ b/tests/api-resources/apps/users/sessions/sessions.test.ts @@ -3,7 +3,10 @@ import Honcho from 'honcho'; import { Response } from 'node-fetch'; -const honcho = new Honcho({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' }); +const honcho = new Honcho({ + apiKey: 'My API Key', + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', +}); describe('resource sessions', () => { test('create: only required params', async () => { diff --git a/tests/api-resources/apps/users/users.test.ts b/tests/api-resources/apps/users/users.test.ts index 01aa123..bea3eb2 100644 --- a/tests/api-resources/apps/users/users.test.ts +++ b/tests/api-resources/apps/users/users.test.ts @@ -3,7 +3,10 @@ import Honcho from 'honcho'; import { Response } from 'node-fetch'; -const honcho = new Honcho({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' }); +const honcho = new Honcho({ + apiKey: 'My API Key', + baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', +}); describe('resource users', () => { test('create: only required params', async () => { diff --git a/tests/index.test.ts b/tests/index.test.ts index 7aca851..dff87d4 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -23,6 +23,7 @@ describe('instantiate client', () => { const client = new Honcho({ baseURL: 'http://localhost:5000/', defaultHeaders: { 'X-My-Default-Header': '2' }, + apiKey: 'My API Key', }); test('they are used in the request', () => { @@ -51,7 +52,11 @@ describe('instantiate client', () => { describe('defaultQuery', () => { test('with null query params given', () => { - const client = new Honcho({ baseURL: 'http://localhost:5000/', defaultQuery: { apiVersion: 'foo' } }); + const client = new Honcho({ + baseURL: 'http://localhost:5000/', + defaultQuery: { apiVersion: 'foo' }, + apiKey: 'My API Key', + }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo'); }); @@ -59,12 +64,17 @@ describe('instantiate client', () => { const client = new Honcho({ baseURL: 'http://localhost:5000/', defaultQuery: { apiVersion: 'foo', hello: 'world' }, + apiKey: 'My API Key', }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo&hello=world'); }); test('overriding with `undefined`', () => { - const client = new Honcho({ baseURL: 'http://localhost:5000/', defaultQuery: { hello: 'world' } }); + const client = new Honcho({ + baseURL: 'http://localhost:5000/', + defaultQuery: { hello: 'world' }, + apiKey: 'My API Key', + }); expect(client.buildURL('/foo', { hello: undefined })).toEqual('http://localhost:5000/foo'); }); }); @@ -72,6 +82,7 @@ describe('instantiate client', () => { test('custom fetch', async () => { const client = new Honcho({ baseURL: 'http://localhost:5000/', + apiKey: 'My API Key', fetch: (url) => { return Promise.resolve( new Response(JSON.stringify({ url, custom: true }), { @@ -88,6 +99,7 @@ describe('instantiate client', () => { test('custom signal', async () => { const client = new Honcho({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', + apiKey: 'My API Key', fetch: (...args) => { return new Promise((resolve, reject) => setTimeout( @@ -112,12 +124,12 @@ describe('instantiate client', () => { describe('baseUrl', () => { test('trailing slash', () => { - const client = new Honcho({ baseURL: 'http://localhost:5000/custom/path/' }); + const client = new Honcho({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'My API Key' }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo'); }); test('no trailing slash', () => { - const client = new Honcho({ baseURL: 'http://localhost:5000/custom/path' }); + const client = new Honcho({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'My API Key' }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo'); }); @@ -126,41 +138,55 @@ describe('instantiate client', () => { }); test('explicit option', () => { - const client = new Honcho({ baseURL: 'https://example.com' }); + const client = new Honcho({ baseURL: 'https://example.com', apiKey: 'My API Key' }); expect(client.baseURL).toEqual('https://example.com'); }); test('env variable', () => { process.env['HONCHO_BASE_URL'] = 'https://example.com/from_env'; - const client = new Honcho({}); + const client = new Honcho({ apiKey: 'My API Key' }); expect(client.baseURL).toEqual('https://example.com/from_env'); }); test('empty env variable', () => { process.env['HONCHO_BASE_URL'] = ''; // empty - const client = new Honcho({}); + const client = new Honcho({ apiKey: 'My API Key' }); expect(client.baseURL).toEqual('https://localhost:8080/test-api'); }); test('blank env variable', () => { process.env['HONCHO_BASE_URL'] = ' '; // blank - const client = new Honcho({}); + const client = new Honcho({ apiKey: 'My API Key' }); expect(client.baseURL).toEqual('https://localhost:8080/test-api'); }); }); test('maxRetries option is correctly set', () => { - const client = new Honcho({ maxRetries: 4 }); + const client = new Honcho({ maxRetries: 4, apiKey: 'My API Key' }); expect(client.maxRetries).toEqual(4); // default - const client2 = new Honcho({}); + const client2 = new Honcho({ apiKey: 'My API Key' }); expect(client2.maxRetries).toEqual(2); }); + + test('with environment variable arguments', () => { + // set options via env var + process.env['HONCHO_AUTH_TOKEN'] = 'My API Key'; + const client = new Honcho(); + expect(client.apiKey).toBe('My API Key'); + }); + + test('with overriden environment variable arguments', () => { + // set options via env var + process.env['HONCHO_AUTH_TOKEN'] = 'another My API Key'; + const client = new Honcho({ apiKey: 'My API Key' }); + expect(client.apiKey).toBe('My API Key'); + }); }); describe('request building', () => { - const client = new Honcho({}); + const client = new Honcho({ apiKey: 'My API Key' }); describe('Content-Length', () => { test('handles multi-byte characters', () => { @@ -202,7 +228,7 @@ describe('retries', () => { return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } }); }; - const client = new Honcho({ timeout: 10, fetch: testFetch }); + const client = new Honcho({ apiKey: 'My API Key', timeout: 10, fetch: testFetch }); expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 }); expect(count).toEqual(2); @@ -229,7 +255,7 @@ describe('retries', () => { return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } }); }; - const client = new Honcho({ fetch: testFetch }); + const client = new Honcho({ apiKey: 'My API Key', fetch: testFetch }); expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 }); expect(count).toEqual(2); @@ -256,7 +282,7 @@ describe('retries', () => { return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } }); }; - const client = new Honcho({ fetch: testFetch }); + const client = new Honcho({ apiKey: 'My API Key', fetch: testFetch }); expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 }); expect(count).toEqual(2);