Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): getXXX methods return instances #38

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import {
Prompt,
Score,
Step,
StepConstructor,
StepType,
Thread,
User,
Utils
} from './types';
Expand Down Expand Up @@ -467,7 +467,7 @@ export class API {
before?: Maybe<string>;
filters?: StepsFilter[];
orderBy?: StepsOrderBy;
}): Promise<PaginatedResponse<OmitUtils<StepConstructor>>> {
}): Promise<PaginatedResponse<Step>> {
const query = `
query GetSteps(
$after: ID,
Expand Down Expand Up @@ -509,7 +509,7 @@ export class API {

const response = result.data.steps;

response.data = response.edges.map((x: any) => x.node);
response.data = response.edges.map((x: any) => new Step(this, x.node));
delete response.edges;

return response;
Expand Down Expand Up @@ -537,9 +537,11 @@ export class API {

const result = await this.makeGqlCall(query, variables);

const step = result.data.step;
if (!result.data.step) {
return null;
}

return step;
return new Step(this, result.data.step);
}

/**
Expand Down Expand Up @@ -876,7 +878,7 @@ export class API {
};

const response = await this.makeGqlCall(query, variables);
return response.data.upsertThread;
return new Thread(this, response.data.upsertThread);
}

/**
Expand All @@ -897,7 +899,7 @@ export class API {
filters?: ThreadsFilter[];
orderBy?: ThreadsOrderBy;
stepTypesToKeep?: StepType[];
}): Promise<PaginatedResponse<CleanThreadFields>> {
}): Promise<PaginatedResponse<Thread>> {
const query = `
query GetThreads(
$after: ID,
Expand Down Expand Up @@ -941,7 +943,7 @@ export class API {

const response = result.data.threads;

response.data = response.edges.map((x: any) => x.node);
response.data = response.edges.map((x: any) => new Thread(this, x.node));
delete response.edges;

return response;
Expand All @@ -953,7 +955,7 @@ export class API {
* @param id - The unique identifier of the thread. This parameter is required.
* @returns The detailed information of the specified thread.
*/
async getThread(id: string) {
async getThread(id: string): Promise<Maybe<Thread>> {
const query = `
query GetThread($id: String!) {
threadDetail(id: $id) {
Expand All @@ -965,7 +967,12 @@ export class API {
const variables = { id };

const response = await this.makeGqlCall(query, variables);
return response.data.threadDetail;

if (!response.data.threadDetail) {
return null;
}

return new Thread(this, response.data.threadDetail);
}

/**
Expand All @@ -974,7 +981,7 @@ export class API {
* @param id - The unique identifier of the thread to be deleted. This parameter is required.
* @returns The ID of the deleted thread.
*/
async deleteThread(id: string) {
async deleteThread(id: string): Promise<string> {
const query = `
mutation DeleteThread($threadId: String!) {
deleteThread(id: $threadId) {
Expand Down Expand Up @@ -1051,7 +1058,7 @@ export class API {

const response = result.data.participants;

response.data = response.edges.map((x: any) => x.node);
response.data = response.edges.map((x: any) => new User(x.node));
delete response.edges;

return response;
Expand Down Expand Up @@ -1273,7 +1280,7 @@ export class API {

const response = result.data.scores;

response.data = response.edges.map((x: any) => x.node);
response.data = response.edges.map((x: any) => new Score(x.node));
delete response.edges;

return response;
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('End to end tests for the SDK', function () {
expect(thread.metadata).toStrictEqual({ foo: 'bar' });

const fetchedThread = await client.api.getThread(thread.id);
expect(fetchedThread.id).toBe(thread.id);
expect(fetchedThread?.id).toBe(thread.id);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only "breaking" change i could identify. Existing (erroneous) code may rely on the fact that getThread used to be typed any and not include any null checks.


const updatedThread = await client.api.upsertThread({
threadId: thread.id,
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('End to end tests for the SDK', function () {
expect(thread.metadata).toStrictEqual({ foo: 'bar' });

const fetchedThread = await client.api.getThread(thread.id);
expect(fetchedThread.id).toBe(thread.id);
expect(fetchedThread?.id).toBe(thread.id);

const updatedThread = await client.api.upsertThread(
thread.id,
Expand Down
Loading