@youdotcom-oss/api 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@youdotcom-oss/api",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "You.com API client with bundled CLI for agents supporting Agent Skills",
5
5
  "license": "MIT",
6
6
  "engines": {
package/src/main.ts CHANGED
@@ -16,8 +16,9 @@ export * from './deep-search/deep-search.utils.ts'
16
16
  // Search
17
17
  export * from './search/search.schemas.ts'
18
18
  export * from './search/search.utils.ts'
19
- // Shared
20
19
  export * from './shared/api.constants.ts'
21
20
  export * from './shared/api.types.ts'
21
+ // Shared
22
+ export * from './shared/api-error.schemas.ts'
22
23
  export * from './shared/dry-run-utils.ts'
23
24
  export * from './shared/generate-error-report-link.ts'
@@ -1,5 +1,6 @@
1
1
  import { SEARCH_API_URL } from '../shared/api.constants.ts'
2
2
  import type { GetUserAgent } from '../shared/api.types.ts'
3
+ import { ApiErrorResponseSchema } from '../shared/api-error.schemas.ts'
3
4
  import { checkResponseForErrors } from '../shared/check-response-for-errors.ts'
4
5
  import { type SearchQuery, SearchResponseSchema } from './search.schemas.ts'
5
6
 
@@ -47,16 +48,20 @@ export const fetchSearchResults = async ({
47
48
  let upgradeUrl = 'https://you.com/platform'
48
49
 
49
50
  try {
50
- const errorBody = (await response.json()) as any
51
- if (errorBody?.message) {
52
- errorMessage = errorBody.message
53
- }
54
- if (errorBody?.upgrade_url) {
55
- upgradeUrl = errorBody.upgrade_url
56
- }
57
- if (errorBody?.reset_at) {
58
- const resetDate = new Date(errorBody.reset_at).toLocaleDateString()
59
- errorMessage += ` Limit resets on ${resetDate}.`
51
+ const json = await response.json()
52
+ const parseResult = ApiErrorResponseSchema.safeParse(json)
53
+ if (parseResult.success) {
54
+ const errorBody = parseResult.data
55
+ if (errorBody.message) {
56
+ errorMessage = errorBody.message
57
+ }
58
+ if (errorBody.upgrade_url) {
59
+ upgradeUrl = errorBody.upgrade_url
60
+ }
61
+ if (errorBody.reset_at) {
62
+ const resetDate = new Date(errorBody.reset_at).toLocaleDateString()
63
+ errorMessage += ` Limit resets on ${resetDate}.`
64
+ }
60
65
  }
61
66
  } catch {
62
67
  // If parsing fails, use default message
@@ -0,0 +1,19 @@
1
+ import { z } from 'zod'
2
+
3
+ /**
4
+ * Schema for API error response body (402 Payment Required)
5
+ *
6
+ * @public
7
+ */
8
+ export const ApiErrorResponseSchema = z.object({
9
+ message: z.string().optional(),
10
+ upgrade_url: z.string().url().optional(),
11
+ reset_at: z.string().optional(),
12
+ })
13
+
14
+ /**
15
+ * Type for API error response
16
+ *
17
+ * @public
18
+ */
19
+ export type ApiErrorResponse = z.infer<typeof ApiErrorResponseSchema>