Skip to content

Commit ddcd65d

Browse files
committed
items are also generator
1 parent 6e07bcf commit ddcd65d

File tree

3 files changed

+5
-9
lines changed

3 files changed

+5
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "oneai",
33
"main": "lib/src/index.js",
4-
"version": "0.3.6",
4+
"version": "0.3.7",
55
"repository": "github:power-of-language/oneai-sdk-node",
66
"scripts": {
77
"build": "tsc",

src/clustering.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,18 @@ export namespace ClusteringClient {
134134
this.metadata = metadata;
135135
}
136136

137-
async getItems(
137+
async* getItems(
138138
params?: {
139139
itemMetadata?: string,
140140
},
141-
): Promise<Item[]> {
141+
): AsyncGenerator<Item, void, undefined> {
142142
const urlParams = new URLSearchParams();
143143
if (params?.itemMetadata !== undefined) urlParams.set('item-metadata', params?.itemMetadata!);
144144
const { data } = await this.cluster.collection.client.GET(
145145
`${this.cluster.collection.name}/phrases/${this.id}/items?${urlParams}`,
146146
this.cluster.collection.apiKey,
147147
);
148-
return data ? data.map((item: any) => Item.fromJson(this, item)) : [];
148+
yield* data ? data.map((item: any) => Item.fromJson(this, item)) : [];
149149
}
150150

151151
static fromJson(cluster: Cluster, phrase: any): Phrase {

test/clustering.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ describe('Clustering', () => {
4242
console.log(`\t${cluster.text}`);
4343
const phrase = (await cluster.getPhrases().next()).value;
4444
console.log(`\t\t${phrase.text}`);
45-
try {
46-
const item = (await phrase.getItems())[0];
45+
for await (const item of phrase.getItems()) {
4746
console.log(`\t\t\t${item.text}`);
48-
} catch (e) {
49-
console.log(e);
50-
throw e;
5147
}
5248
}
5349
}

0 commit comments

Comments
 (0)