@youdotcom-oss/api 0.2.2 → 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.2.2",
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
 
@@ -42,6 +43,31 @@ export const fetchSearchResults = async ({
42
43
  throw new Error('Rate limited by You.com API. Please try again later.')
43
44
  } else if (errorCode === 403) {
44
45
  throw new Error('Forbidden. Please check your You.com API key.')
46
+ } else if (errorCode === 402) {
47
+ let errorMessage = 'Free tier limit exceeded. Please upgrade to continue.'
48
+ let upgradeUrl = 'https://you.com/platform'
49
+
50
+ try {
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
+ }
65
+ }
66
+ } catch {
67
+ // If parsing fails, use default message
68
+ }
69
+
70
+ throw new Error(`${errorMessage} Upgrade at: ${upgradeUrl}`)
45
71
  }
46
72
 
47
73
  throw new Error(`Failed to perform search. Error code: ${errorCode}`)
@@ -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>
@@ -5,6 +5,6 @@
5
5
  * Exported for use in tests and external packages.
6
6
  */
7
7
 
8
- export const SEARCH_API_URL = 'https://ydc-index.io/v1/search'
9
- export const DEEP_SEARCH_API_URL = 'https://api.you.com/v1/deep_search'
10
- export const CONTENTS_API_URL = 'https://ydc-index.io/v1/contents'
8
+ export const SEARCH_API_URL = process.env.YDC_SEARCH_API_URL || 'https://ydc-index.io/v1/search'
9
+ export const DEEP_SEARCH_API_URL = process.env.YDC_DEEP_SEARCH_API_URL || 'https://api.you.com/v1/deep_search'
10
+ export const CONTENTS_API_URL = process.env.YDC_CONTENTS_API_URL || 'https://ydc-index.io/v1/contents'