@youdotcom-oss/api 0.3.0 → 0.3.2

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.2",
4
4
  "description": "You.com API client with bundled CLI for agents supporting Agent Skills",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -6,7 +6,21 @@ import * as z from 'zod'
6
6
  */
7
7
  export const ContentsQuerySchema = z.object({
8
8
  urls: z
9
- .array(z.string().url())
9
+ .array(
10
+ // Use .refine() instead of .url() to ensure JSON schema includes "type": "string"
11
+ // This is required for OpenAI function calling schema validation
12
+ z.string().refine(
13
+ (val) => {
14
+ try {
15
+ new URL(val)
16
+ return true
17
+ } catch {
18
+ return false
19
+ }
20
+ },
21
+ { message: 'Invalid URL format' },
22
+ ),
23
+ )
10
24
  .min(1)
11
25
  .describe('Array of webpage URLs to extract content from (e.g., ["https://example.com"])'),
12
26
  formats: z
@@ -24,6 +24,7 @@ describe('ContentsQuerySchema OpenAPI validation', () => {
24
24
  {}, // Missing urls
25
25
  { urls: [] }, // Empty urls array
26
26
  { urls: ['not-a-url'] }, // Invalid URL
27
+ { urls: [''] }, // Empty URL string
27
28
  { urls: ['https://example.com'], formats: ['invalid'] }, // Invalid format
28
29
  { urls: ['https://example.com'], crawl_timeout: 0 }, // Timeout too low
29
30
  { urls: ['https://example.com'], crawl_timeout: 61 }, // Timeout too high
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,34 @@
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
+ // Use .refine() instead of .url() to ensure JSON schema includes "type": "string"
11
+ // This is required for OpenAI function calling schema validation
12
+ upgrade_url: z
13
+ .string()
14
+ .refine(
15
+ (val) => {
16
+ try {
17
+ new URL(val)
18
+ return true
19
+ } catch {
20
+ return false
21
+ }
22
+ },
23
+ { message: 'Invalid URL format' },
24
+ )
25
+ .optional(),
26
+ reset_at: z.string().optional(),
27
+ })
28
+
29
+ /**
30
+ * Type for API error response
31
+ *
32
+ * @public
33
+ */
34
+ export type ApiErrorResponse = z.infer<typeof ApiErrorResponseSchema>