@seyaq/sdk 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Abdulrahman Al Fuhaydi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # `@seyaq/sdk`
2
+
3
+ Server-side TypeScript client for the [Seyaq Search API](https://seyaq.analfuhaydi.workers.dev). Do not put Seyaq API keys in browser bundles.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @seyaq/sdk
9
+ ```
10
+
11
+ Node.js 18 or newer is supported. The package also works in runtimes that provide `fetch`, `crypto.randomUUID`, and `AbortSignal`, including Cloudflare Workers.
12
+
13
+ ## Search
14
+
15
+ ```ts
16
+ import { Seyaq } from "@seyaq/sdk";
17
+
18
+ const seyaq = new Seyaq({ apiKey: process.env.SEYAQ_API_KEY! });
19
+ const response = await seyaq.search({
20
+ query: "renewable energy",
21
+ language: "en",
22
+ country: "US",
23
+ limit: 5,
24
+ safeSearch: "moderate",
25
+ });
26
+
27
+ for (const result of response.results) {
28
+ console.log(result.title, result.url);
29
+ }
30
+ ```
31
+
32
+ Seyaq trims the query, normalizes language and country casing, and rejects invalid input with `TypeError` before sending a request.
33
+
34
+ ## Handle API errors
35
+
36
+ ```ts
37
+ import { Seyaq, SeyaqError } from "@seyaq/sdk";
38
+
39
+ const seyaq = new Seyaq({ apiKey: process.env.SEYAQ_API_KEY! });
40
+
41
+ try {
42
+ await seyaq.search({ query: "أخبار التقنية", language: "ar", country: "SA" });
43
+ } catch (error) {
44
+ if (error instanceof SeyaqError) {
45
+ console.error(error.status, error.code, error.requestId, error.retryAt);
46
+ }
47
+ throw error;
48
+ }
49
+ ```
50
+
51
+ ## Cancel a request
52
+
53
+ ```ts
54
+ const controller = new AbortController();
55
+ const request = seyaq.search(
56
+ { query: "climate policy", language: "en", country: "GB" },
57
+ { signal: controller.signal },
58
+ );
59
+
60
+ controller.abort();
61
+ await request;
62
+ ```
63
+
64
+ ## Supply an idempotency key
65
+
66
+ The SDK creates a random key for each request. Supply your own when you need to identify a request across retries. Reusing a processed key returns `409 REQUEST_ALREADY_PROCESSED`; Seyaq does not charge the daily quota twice.
67
+
68
+ ```ts
69
+ await seyaq.search(
70
+ { query: "Saudi startup funding", language: "en", country: "SA" },
71
+ { idempotencyKey: "job-42-search-1" },
72
+ );
73
+ ```
74
+
75
+ ## Use a different API endpoint
76
+
77
+ ```ts
78
+ const seyaq = new Seyaq({
79
+ apiKey: process.env.SEYAQ_API_KEY!,
80
+ baseUrl: "https://example.internal",
81
+ });
82
+ ```
83
+
84
+ ## Cloudflare Worker
85
+
86
+ ```ts
87
+ import { Seyaq } from "@seyaq/sdk";
88
+
89
+ interface Env {
90
+ SEYAQ_API_KEY: string;
91
+ }
92
+
93
+ export default {
94
+ async fetch(_request: Request, env: Env) {
95
+ const seyaq = new Seyaq({ apiKey: env.SEYAQ_API_KEY });
96
+ const response = await seyaq.search({
97
+ query: "الطاقة الشمسية",
98
+ language: "ar",
99
+ country: "SA",
100
+ });
101
+ return Response.json(response);
102
+ },
103
+ } satisfies ExportedHandler<Env>;
104
+ ```
105
+
106
+ ## License
107
+
108
+ MIT
@@ -0,0 +1,64 @@
1
+ export declare const SEARCH_LANGUAGES: readonly ["ar", "bg", "bn", "ca", "cs", "da", "de", "el", "en", "es", "et", "eu", "fi", "fr", "gl", "gu", "he", "hi", "hr", "hu", "it", "ja", "kn", "ko", "lt", "lv", "ml", "mr", "ms", "nl", "no", "pl", "pt-br", "pt-pt", "ro", "ru", "sk", "sl", "sr", "sv", "ta", "te", "th", "tr", "uk", "vi", "zh-hans", "zh-hant"];
2
+ export declare const SEARCH_COUNTRIES: readonly ["ALL", "AR", "AU", "AT", "BE", "BR", "CA", "CL", "DK", "FI", "FR", "DE", "HK", "IN", "ID", "IT", "JP", "KR", "MY", "MX", "NL", "NZ", "NO", "CN", "PL", "PT", "PH", "RU", "SA", "ZA", "ES", "SE", "CH", "TW", "TR", "GB", "US"];
3
+ export declare const ERROR_CODES: readonly ["INVALID_REQUEST", "INVALID_API_KEY", "IDEMPOTENCY_CONFLICT", "REQUEST_IN_PROGRESS", "REQUEST_ALREADY_PROCESSED", "REQUEST_TOO_LARGE", "UNSUPPORTED_MEDIA_TYPE", "DAILY_LIMIT_EXCEEDED", "RATE_LIMITED", "SEARCH_PROVIDER_ERROR", "SEARCH_UNAVAILABLE", "SERVICE_CAPACITY_EXCEEDED", "SEARCH_TIMEOUT", "INTERNAL_ERROR"];
4
+ export type SearchLanguage = (typeof SEARCH_LANGUAGES)[number];
5
+ export type SearchCountry = (typeof SEARCH_COUNTRIES)[number];
6
+ export type Freshness = "day" | "week" | "month" | "year";
7
+ export type SafeSearch = "off" | "moderate" | "strict";
8
+ export type ErrorCode = (typeof ERROR_CODES)[number];
9
+ export interface SearchRequest {
10
+ query: string;
11
+ language: SearchLanguage;
12
+ country: SearchCountry;
13
+ limit?: number;
14
+ freshness?: Freshness;
15
+ safeSearch?: SafeSearch;
16
+ }
17
+ export interface SearchResult {
18
+ title: string;
19
+ url: string;
20
+ description: string;
21
+ publishedAt: string | null;
22
+ }
23
+ export interface Usage {
24
+ charged: boolean;
25
+ remaining: number;
26
+ dailyLimit: number;
27
+ resetsAt: string;
28
+ }
29
+ export interface SearchResponse {
30
+ requestId: string;
31
+ query: string;
32
+ results: SearchResult[];
33
+ usage: Usage;
34
+ }
35
+ export interface ErrorResponse {
36
+ requestId: string;
37
+ error: {
38
+ code: ErrorCode;
39
+ message: string;
40
+ retryAt?: string;
41
+ details?: unknown;
42
+ };
43
+ }
44
+ export interface SeyaqOptions {
45
+ apiKey: string;
46
+ baseUrl?: string;
47
+ }
48
+ export interface SearchOptions {
49
+ idempotencyKey?: string;
50
+ signal?: AbortSignal;
51
+ }
52
+ export declare class SeyaqError extends Error {
53
+ readonly status: number;
54
+ readonly code: ErrorCode | "INVALID_RESPONSE";
55
+ readonly requestId?: string | undefined;
56
+ readonly retryAt?: string | undefined;
57
+ constructor(message: string, status: number, code: ErrorCode | "INVALID_RESPONSE", requestId?: string | undefined, retryAt?: string | undefined);
58
+ }
59
+ export declare class Seyaq {
60
+ private readonly options;
61
+ private readonly baseUrl;
62
+ constructor(options: SeyaqOptions);
63
+ search(input: SearchRequest, options?: SearchOptions): Promise<SearchResponse>;
64
+ }
package/dist/index.js ADDED
@@ -0,0 +1,240 @@
1
+ export const SEARCH_LANGUAGES = [
2
+ "ar",
3
+ "bg",
4
+ "bn",
5
+ "ca",
6
+ "cs",
7
+ "da",
8
+ "de",
9
+ "el",
10
+ "en",
11
+ "es",
12
+ "et",
13
+ "eu",
14
+ "fi",
15
+ "fr",
16
+ "gl",
17
+ "gu",
18
+ "he",
19
+ "hi",
20
+ "hr",
21
+ "hu",
22
+ "it",
23
+ "ja",
24
+ "kn",
25
+ "ko",
26
+ "lt",
27
+ "lv",
28
+ "ml",
29
+ "mr",
30
+ "ms",
31
+ "nl",
32
+ "no",
33
+ "pl",
34
+ "pt-br",
35
+ "pt-pt",
36
+ "ro",
37
+ "ru",
38
+ "sk",
39
+ "sl",
40
+ "sr",
41
+ "sv",
42
+ "ta",
43
+ "te",
44
+ "th",
45
+ "tr",
46
+ "uk",
47
+ "vi",
48
+ "zh-hans",
49
+ "zh-hant",
50
+ ];
51
+ export const SEARCH_COUNTRIES = [
52
+ "ALL",
53
+ "AR",
54
+ "AU",
55
+ "AT",
56
+ "BE",
57
+ "BR",
58
+ "CA",
59
+ "CL",
60
+ "DK",
61
+ "FI",
62
+ "FR",
63
+ "DE",
64
+ "HK",
65
+ "IN",
66
+ "ID",
67
+ "IT",
68
+ "JP",
69
+ "KR",
70
+ "MY",
71
+ "MX",
72
+ "NL",
73
+ "NZ",
74
+ "NO",
75
+ "CN",
76
+ "PL",
77
+ "PT",
78
+ "PH",
79
+ "RU",
80
+ "SA",
81
+ "ZA",
82
+ "ES",
83
+ "SE",
84
+ "CH",
85
+ "TW",
86
+ "TR",
87
+ "GB",
88
+ "US",
89
+ ];
90
+ export const ERROR_CODES = [
91
+ "INVALID_REQUEST",
92
+ "INVALID_API_KEY",
93
+ "IDEMPOTENCY_CONFLICT",
94
+ "REQUEST_IN_PROGRESS",
95
+ "REQUEST_ALREADY_PROCESSED",
96
+ "REQUEST_TOO_LARGE",
97
+ "UNSUPPORTED_MEDIA_TYPE",
98
+ "DAILY_LIMIT_EXCEEDED",
99
+ "RATE_LIMITED",
100
+ "SEARCH_PROVIDER_ERROR",
101
+ "SEARCH_UNAVAILABLE",
102
+ "SERVICE_CAPACITY_EXCEEDED",
103
+ "SEARCH_TIMEOUT",
104
+ "INTERNAL_ERROR",
105
+ ];
106
+ const languageSet = new Set(SEARCH_LANGUAGES);
107
+ const countrySet = new Set(SEARCH_COUNTRIES);
108
+ const freshnessSet = new Set(["day", "week", "month", "year"]);
109
+ const safeSearchSet = new Set(["off", "moderate", "strict"]);
110
+ const errorCodeSet = new Set(ERROR_CODES);
111
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
112
+ const hasOnlyKeys = (value, keys) => Object.keys(value).every((key) => keys.includes(key));
113
+ const isNonNegativeInteger = (value) => Number.isInteger(value) && Number(value) >= 0;
114
+ function parseSearchRequest(input) {
115
+ if (!isObject(input) ||
116
+ !hasOnlyKeys(input, [
117
+ "query",
118
+ "language",
119
+ "country",
120
+ "limit",
121
+ "freshness",
122
+ "safeSearch",
123
+ ]))
124
+ throw new TypeError("Invalid search request");
125
+ const query = typeof input.query === "string" ? input.query.trim() : "";
126
+ const language = typeof input.language === "string" ? input.language.toLowerCase() : "";
127
+ const country = typeof input.country === "string" ? input.country.toUpperCase() : "";
128
+ if (query.length < 1 ||
129
+ query.length > 600 ||
130
+ !languageSet.has(language) ||
131
+ !countrySet.has(country))
132
+ throw new TypeError("Invalid search request");
133
+ if (input.limit !== undefined &&
134
+ (!Number.isInteger(input.limit) ||
135
+ Number(input.limit) < 1 ||
136
+ Number(input.limit) > 10))
137
+ throw new TypeError("Invalid search request");
138
+ if (input.freshness !== undefined &&
139
+ (typeof input.freshness !== "string" || !freshnessSet.has(input.freshness)))
140
+ throw new TypeError("Invalid search request");
141
+ if (input.safeSearch !== undefined &&
142
+ (typeof input.safeSearch !== "string" ||
143
+ !safeSearchSet.has(input.safeSearch)))
144
+ throw new TypeError("Invalid search request");
145
+ return {
146
+ query,
147
+ language: language,
148
+ country: country,
149
+ limit: input.limit === undefined ? 5 : Number(input.limit),
150
+ ...(input.freshness === undefined
151
+ ? {}
152
+ : { freshness: input.freshness }),
153
+ safeSearch: input.safeSearch === undefined
154
+ ? "moderate"
155
+ : input.safeSearch,
156
+ };
157
+ }
158
+ function parseErrorResponse(value) {
159
+ if (!isObject(value) ||
160
+ typeof value.requestId !== "string" ||
161
+ !isObject(value.error))
162
+ return;
163
+ const error = value.error;
164
+ if (typeof error.code !== "string" ||
165
+ !errorCodeSet.has(error.code) ||
166
+ typeof error.message !== "string" ||
167
+ (error.retryAt !== undefined && typeof error.retryAt !== "string"))
168
+ return;
169
+ return value;
170
+ }
171
+ function parseSearchResponse(value) {
172
+ if (!isObject(value) ||
173
+ typeof value.requestId !== "string" ||
174
+ typeof value.query !== "string" ||
175
+ !Array.isArray(value.results) ||
176
+ !isObject(value.usage))
177
+ return;
178
+ if (!value.results.every((result) => isObject(result) &&
179
+ typeof result.title === "string" &&
180
+ typeof result.url === "string" &&
181
+ typeof result.description === "string" &&
182
+ (result.publishedAt === null || typeof result.publishedAt === "string")))
183
+ return;
184
+ const usage = value.usage;
185
+ if (typeof usage.charged !== "boolean" ||
186
+ !isNonNegativeInteger(usage.remaining) ||
187
+ !Number.isInteger(usage.dailyLimit) ||
188
+ Number(usage.dailyLimit) <= 0 ||
189
+ typeof usage.resetsAt !== "string")
190
+ return;
191
+ return value;
192
+ }
193
+ export class SeyaqError extends Error {
194
+ status;
195
+ code;
196
+ requestId;
197
+ retryAt;
198
+ constructor(message, status, code, requestId, retryAt) {
199
+ super(message);
200
+ this.status = status;
201
+ this.code = code;
202
+ this.requestId = requestId;
203
+ this.retryAt = retryAt;
204
+ this.name = "SeyaqError";
205
+ }
206
+ }
207
+ export class Seyaq {
208
+ options;
209
+ baseUrl;
210
+ constructor(options) {
211
+ this.options = options;
212
+ if (!options.apiKey)
213
+ throw new TypeError("apiKey is required");
214
+ this.baseUrl = (options.baseUrl ?? "https://seyaq.analfuhaydi.workers.dev").replace(/\/+$/, "");
215
+ }
216
+ async search(input, options = {}) {
217
+ const body = parseSearchRequest(input);
218
+ const response = await fetch(`${this.baseUrl}/v1/search`, {
219
+ method: "POST",
220
+ headers: {
221
+ Authorization: `Bearer ${this.options.apiKey}`,
222
+ "Content-Type": "application/json",
223
+ "Idempotency-Key": options.idempotencyKey ?? crypto.randomUUID(),
224
+ },
225
+ body: JSON.stringify(body),
226
+ ...(options.signal ? { signal: options.signal } : {}),
227
+ });
228
+ const payload = await response.json().catch(() => undefined);
229
+ if (!response.ok) {
230
+ const error = parseErrorResponse(payload);
231
+ if (error)
232
+ throw new SeyaqError(error.error.message, response.status, error.error.code, error.requestId, error.error.retryAt);
233
+ throw new SeyaqError("Seyaq returned an invalid error response", response.status, "INVALID_RESPONSE", response.headers.get("X-Request-ID") ?? undefined);
234
+ }
235
+ const result = parseSearchResponse(payload);
236
+ if (result)
237
+ return result;
238
+ throw new SeyaqError("Seyaq returned an invalid response", response.status, "INVALID_RESPONSE", response.headers.get("X-Request-ID") ?? undefined);
239
+ }
240
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@seyaq/sdk",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/analfuhaydi/seyaq.git",
22
+ "directory": "packages/sdk"
23
+ },
24
+ "homepage": "https://seyaq.analfuhaydi.workers.dev",
25
+ "bugs": {
26
+ "url": "https://github.com/analfuhaydi/seyaq/issues"
27
+ },
28
+ "keywords": [
29
+ "search",
30
+ "api",
31
+ "typescript",
32
+ "cloudflare-workers",
33
+ "seyaq"
34
+ ],
35
+ "engines": {
36
+ "node": ">=18"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "devDependencies": {
42
+ "typescript": "5.9.2"
43
+ },
44
+ "scripts": {
45
+ "build": "tsc -p tsconfig.build.json",
46
+ "check": "tsc --noEmit",
47
+ "pack:check": "pnpm pack --pack-destination .packs"
48
+ }
49
+ }