Skip to content

Commit 6508862

Browse files
committed
fix(ts): modify types for response
modify typescript types for the response to adapt search results
1 parent bc37dfc commit 6508862

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/hyper.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Types } from './types';
2+
import { Search } from './search';
23

34
const baseUrl = process.env.HYPER_BASE_URL || 'https://api.gethyper.ai/v1';
45

@@ -12,6 +13,7 @@ export class Hyper {
1213
private readonly headers: Headers;
1314

1415
readonly types = new Types(this);
16+
readonly search = new Search(this);
1517

1618
constructor(private readonly apiKey?: string) {
1719
if (!apiKey) {
@@ -36,7 +38,16 @@ export class Hyper {
3638
}: {
3739
endpoint: string;
3840
options?: RequestInit;
39-
}): Promise<{ data: T | null; error: string | null }> {
41+
}): Promise<
42+
| {
43+
data: T;
44+
error: null;
45+
}
46+
| {
47+
data: null;
48+
error: string;
49+
}
50+
> {
4051
const res = await fetch(`${baseUrl}${endpoint}`, options);
4152

4253
if (!res.ok) {
@@ -45,7 +56,7 @@ export class Hyper {
4556
return { data: null, error };
4657
}
4758

48-
const { data } = (await res.json()) as { data: T };
59+
const data = (await res.json()) as T;
4960
return { data, error: null };
5061
}
5162

src/search/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Hyper } from '../hyper';
2-
import { MethodParamsOptions, SearchResultData } from './@types';
2+
import type { MethodParamsOptions, SearchResultData } from './@types';
33

44
export class Search {
55
constructor(private readonly hyper: Hyper) {}

src/types/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export class Types {
3636
? { query: query, context_id: contextId }
3737
: { content: query };
3838

39-
const res = await this.hyper.post<T>({
39+
const res = await this.hyper.post<{ data: T }>({
4040
endpoint,
4141
body: payload,
4242
});
4343

44-
return res;
44+
if (res.data?.data) return { data: res.data.data, error: null };
45+
46+
return { data: null, error: res.error };
4547
}
4648

4749
async string(query: string, options?: MethodParamsOptions) {

0 commit comments

Comments
 (0)