@aicats/sdk 1.0.1 → 1.0.4

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/README.md CHANGED
@@ -30,26 +30,39 @@ Get a random AI-generated cat image.
30
30
 
31
31
  ```typescript
32
32
  const blob = await AiCats.random({
33
- size: Size.Medium, // Image size (default: Large)
34
- theme: Theme.Xmas // Optional theme
33
+ size: Size.Medium, // Image size (default: Large)
34
+ theme: Theme.Xmas, // Optional theme
35
+ responseType: 'blob' // 'blob' | 'arrayBuffer' | 'base64' | 'dataUrl'
35
36
  });
37
+
38
+ // Get as base64
39
+ const base64 = await AiCats.random({ responseType: 'base64' });
40
+
41
+ // Get as data URL (for direct use in img.src)
42
+ const dataUrl = await AiCats.random({ responseType: 'dataUrl' });
36
43
  ```
37
44
 
38
- ### `AiCats.getById(id, size?)`
45
+ ### `AiCats.getById(id, options?)`
39
46
  Get a specific cat image by ID.
40
47
 
41
48
  ```typescript
42
- const blob = await AiCats.getById('abc-123-def', Size.Small);
49
+ const blob = await AiCats.getById('669de24a-1da1-4fcd-84b1-9e55a43a0e0e', { size: Size.Small });
50
+
51
+ // Get as base64
52
+ const base64 = await AiCats.getById('669de24a-1da1-4fcd-84b1-9e55a43a0e0e', { responseType: 'base64' });
53
+
54
+ // Get as data URL
55
+ const dataUrl = await AiCats.getById('669de24a-1da1-4fcd-84b1-9e55a43a0e0e', { responseType: 'dataUrl' });
43
56
  ```
44
57
 
45
58
  ### `AiCats.getInfo(id)`
46
59
  Get detailed information about a cat image.
47
60
 
48
61
  ```typescript
49
- const info = await AiCats.getInfo('abc-123-def');
50
- console.log(info.prompt); // "A fluffy orange cat..."
51
- console.log(info.theme); // "Halloween"
52
- console.log(info.dateCreated); // 1699012345678
62
+ const info = await AiCats.getInfo('669de24a-1da1-4fcd-84b1-9e55a43a0e0e');
63
+ console.log(info.prompt); // "In a futuristic space observatory..."
64
+ console.log(info.theme); // "Default"
65
+ console.log(info.dateCreated); // 1724534067586
53
66
  ```
54
67
 
55
68
  ### `AiCats.search(options?)`
@@ -57,10 +70,10 @@ Search for cat images.
57
70
 
58
71
  ```typescript
59
72
  const results = await AiCats.search({
60
- query: 'black cat', // Search text
61
- limit: 20, // Max results (1-100)
62
- theme: Theme.Halloween,// Filter by theme
63
- descending: true // Newest first
73
+ query: 'rainbow', // Search text
74
+ limit: 20, // Max results (1-100)
75
+ theme: Theme.Halloween, // Filter by theme
76
+ descending: true // Newest first
64
77
  });
65
78
 
66
79
  for (const cat of results) {
@@ -72,7 +85,7 @@ for (const cat of results) {
72
85
  Find cats similar to a given cat.
73
86
 
74
87
  ```typescript
75
- const similar = await AiCats.getSimilar('abc-123-def', { limit: 5 });
88
+ const similar = await AiCats.getSimilar('669de24a-1da1-4fcd-84b1-9e55a43a0e0e', { limit: 5 });
76
89
  ```
77
90
 
78
91
  ### `AiCats.getThemes()`
@@ -93,6 +106,14 @@ const halloweenCount = await AiCats.getCount(Theme.Halloween);
93
106
 
94
107
  ## Types
95
108
 
109
+ ### ResponseType
110
+ ```typescript
111
+ 'blob' // Blob object (default)
112
+ 'arrayBuffer' // ArrayBuffer
113
+ 'base64' // Base64 encoded string
114
+ 'dataUrl' // Data URL (data:image/jpeg;base64,...)
115
+ ```
116
+
96
117
  ### Size
97
118
  ```typescript
98
119
  Size.Large // 1024x1024 (default)
package/dist/index.d.mts CHANGED
@@ -38,6 +38,8 @@ declare enum Theme {
38
38
  interface CatInfo {
39
39
  /** Unique identifier for the cat image */
40
40
  id: string;
41
+ /** URL to access the cat image */
42
+ url: string;
41
43
  /** Unix timestamp when the image was created */
42
44
  dateCreated: number;
43
45
  /** The AI prompt used to generate this cat image */
@@ -66,12 +68,18 @@ declare enum Size {
66
68
  Micro = "16"
67
69
  }
68
70
 
71
+ /** Response type for image requests */
72
+ type ResponseType = 'blob' | 'arrayBuffer' | 'base64' | 'dataUrl';
73
+ /** Image response based on responseType */
74
+ type ImageResponse<T extends ResponseType = 'blob'> = T extends 'blob' ? Blob : T extends 'arrayBuffer' ? ArrayBuffer : T extends 'base64' ? string : T extends 'dataUrl' ? string : Blob;
69
75
  /** Options for getting a random cat */
70
- interface RandomCatOptions {
76
+ interface RandomCatOptions<T extends ResponseType = 'blob'> {
71
77
  /** Image size (default: Large) */
72
78
  size?: Size;
73
79
  /** Theme of the cat image */
74
80
  theme?: Theme;
81
+ /** Response format (default: blob) */
82
+ responseType?: T;
75
83
  }
76
84
  /** Options for searching cats */
77
85
  interface SearchOptions {
@@ -95,15 +103,22 @@ interface SimilarOptions {
95
103
  /** Image size in results */
96
104
  size?: Size;
97
105
  }
106
+ /** Options for getting a cat by ID */
107
+ interface GetByIdOptions<T extends ResponseType = 'blob'> {
108
+ /** Image size (default: Large) */
109
+ size?: Size;
110
+ /** Response format (default: blob) */
111
+ responseType?: T;
112
+ }
98
113
 
99
114
  declare const AiCats: {
100
- random: (options?: RandomCatOptions) => Promise<Blob>;
101
- getById: (id: string, size?: Size) => Promise<Blob>;
115
+ random: <T extends ResponseType = "blob">(options?: RandomCatOptions<T>) => Promise<ImageResponse<T>>;
116
+ getById: <T extends ResponseType = "blob">(id: string, options?: GetByIdOptions<T>) => Promise<ImageResponse<T>>;
102
117
  getInfo: (id: string) => Promise<CatInfo>;
103
118
  search: (options?: SearchOptions) => Promise<SearchResult[]>;
104
119
  getSimilar: (id: string, options?: SimilarOptions) => Promise<SearchResult[]>;
105
120
  getSearchCompletion: (options?: SearchOptions) => Promise<string>;
106
- getThemes: () => Promise<string[]>;
121
+ getThemes: () => Promise<Theme[]>;
107
122
  getCount: (theme?: Theme) => Promise<number>;
108
123
  };
109
124
 
package/dist/index.d.ts CHANGED
@@ -38,6 +38,8 @@ declare enum Theme {
38
38
  interface CatInfo {
39
39
  /** Unique identifier for the cat image */
40
40
  id: string;
41
+ /** URL to access the cat image */
42
+ url: string;
41
43
  /** Unix timestamp when the image was created */
42
44
  dateCreated: number;
43
45
  /** The AI prompt used to generate this cat image */
@@ -66,12 +68,18 @@ declare enum Size {
66
68
  Micro = "16"
67
69
  }
68
70
 
71
+ /** Response type for image requests */
72
+ type ResponseType = 'blob' | 'arrayBuffer' | 'base64' | 'dataUrl';
73
+ /** Image response based on responseType */
74
+ type ImageResponse<T extends ResponseType = 'blob'> = T extends 'blob' ? Blob : T extends 'arrayBuffer' ? ArrayBuffer : T extends 'base64' ? string : T extends 'dataUrl' ? string : Blob;
69
75
  /** Options for getting a random cat */
70
- interface RandomCatOptions {
76
+ interface RandomCatOptions<T extends ResponseType = 'blob'> {
71
77
  /** Image size (default: Large) */
72
78
  size?: Size;
73
79
  /** Theme of the cat image */
74
80
  theme?: Theme;
81
+ /** Response format (default: blob) */
82
+ responseType?: T;
75
83
  }
76
84
  /** Options for searching cats */
77
85
  interface SearchOptions {
@@ -95,15 +103,22 @@ interface SimilarOptions {
95
103
  /** Image size in results */
96
104
  size?: Size;
97
105
  }
106
+ /** Options for getting a cat by ID */
107
+ interface GetByIdOptions<T extends ResponseType = 'blob'> {
108
+ /** Image size (default: Large) */
109
+ size?: Size;
110
+ /** Response format (default: blob) */
111
+ responseType?: T;
112
+ }
98
113
 
99
114
  declare const AiCats: {
100
- random: (options?: RandomCatOptions) => Promise<Blob>;
101
- getById: (id: string, size?: Size) => Promise<Blob>;
115
+ random: <T extends ResponseType = "blob">(options?: RandomCatOptions<T>) => Promise<ImageResponse<T>>;
116
+ getById: <T extends ResponseType = "blob">(id: string, options?: GetByIdOptions<T>) => Promise<ImageResponse<T>>;
102
117
  getInfo: (id: string) => Promise<CatInfo>;
103
118
  search: (options?: SearchOptions) => Promise<SearchResult[]>;
104
119
  getSimilar: (id: string, options?: SimilarOptions) => Promise<SearchResult[]>;
105
120
  getSearchCompletion: (options?: SearchOptions) => Promise<string>;
106
- getThemes: () => Promise<string[]>;
121
+ getThemes: () => Promise<Theme[]>;
107
122
  getCount: (theme?: Theme) => Promise<number>;
108
123
  };
109
124
 
package/dist/index.js CHANGED
@@ -54,25 +54,52 @@ var Theme = /* @__PURE__ */ ((Theme2) => {
54
54
 
55
55
  // src/api/v1.api.ts
56
56
  var ApiUrl = "https://api.ai-cats.net/v1";
57
+ async function toResponseType(buffer, type = "blob") {
58
+ switch (type) {
59
+ case "arrayBuffer":
60
+ return buffer;
61
+ case "base64": {
62
+ const bytes = new Uint8Array(buffer);
63
+ let binary = "";
64
+ for (let i = 0; i < bytes.length; i++) {
65
+ binary += String.fromCharCode(bytes[i]);
66
+ }
67
+ return btoa(binary);
68
+ }
69
+ case "dataUrl": {
70
+ const bytes = new Uint8Array(buffer);
71
+ let binary = "";
72
+ for (let i = 0; i < bytes.length; i++) {
73
+ binary += String.fromCharCode(bytes[i]);
74
+ }
75
+ return `data:image/jpeg;base64,${btoa(binary)}`;
76
+ }
77
+ case "blob":
78
+ default:
79
+ return new Blob([buffer], { type: "image/jpeg" });
80
+ }
81
+ }
57
82
  async function random(options) {
58
83
  const params = new URLSearchParams();
59
84
  if (options?.size) params.set("size", options.size);
60
85
  if (options?.theme) params.set("theme", options.theme);
86
+ params.set("rnd", Math.random().toString());
61
87
  const query = params.toString() ? `?${params}` : "";
62
88
  const response = await fetch(`${ApiUrl}/cat${query}`);
63
89
  if (!response.ok) {
64
90
  throw new Error(`Error fetching cat image: ${response.statusText}`);
65
91
  }
66
92
  const buffer = await response.arrayBuffer();
67
- return new Blob([buffer], { type: "image/jpeg" });
93
+ return toResponseType(buffer, options?.responseType ?? "blob");
68
94
  }
69
- async function getById(id, size = "1024" /* Large */) {
95
+ async function getById(id, options) {
96
+ const size = options?.size ?? "1024" /* Large */;
70
97
  const response = await fetch(`${ApiUrl}/cat/${id}?size=${size}`);
71
98
  if (!response.ok) {
72
99
  throw new Error(`Error fetching cat image: ${response.statusText}`);
73
100
  }
74
101
  const buffer = await response.arrayBuffer();
75
- return new Blob([buffer], { type: "image/jpeg" });
102
+ return toResponseType(buffer, options?.responseType ?? "blob");
76
103
  }
77
104
  async function getInfo(id) {
78
105
  const response = await fetch(`${ApiUrl}/cat/info/${id}`);
package/dist/index.mjs CHANGED
@@ -26,25 +26,52 @@ var Theme = /* @__PURE__ */ ((Theme2) => {
26
26
 
27
27
  // src/api/v1.api.ts
28
28
  var ApiUrl = "https://api.ai-cats.net/v1";
29
+ async function toResponseType(buffer, type = "blob") {
30
+ switch (type) {
31
+ case "arrayBuffer":
32
+ return buffer;
33
+ case "base64": {
34
+ const bytes = new Uint8Array(buffer);
35
+ let binary = "";
36
+ for (let i = 0; i < bytes.length; i++) {
37
+ binary += String.fromCharCode(bytes[i]);
38
+ }
39
+ return btoa(binary);
40
+ }
41
+ case "dataUrl": {
42
+ const bytes = new Uint8Array(buffer);
43
+ let binary = "";
44
+ for (let i = 0; i < bytes.length; i++) {
45
+ binary += String.fromCharCode(bytes[i]);
46
+ }
47
+ return `data:image/jpeg;base64,${btoa(binary)}`;
48
+ }
49
+ case "blob":
50
+ default:
51
+ return new Blob([buffer], { type: "image/jpeg" });
52
+ }
53
+ }
29
54
  async function random(options) {
30
55
  const params = new URLSearchParams();
31
56
  if (options?.size) params.set("size", options.size);
32
57
  if (options?.theme) params.set("theme", options.theme);
58
+ params.set("rnd", Math.random().toString());
33
59
  const query = params.toString() ? `?${params}` : "";
34
60
  const response = await fetch(`${ApiUrl}/cat${query}`);
35
61
  if (!response.ok) {
36
62
  throw new Error(`Error fetching cat image: ${response.statusText}`);
37
63
  }
38
64
  const buffer = await response.arrayBuffer();
39
- return new Blob([buffer], { type: "image/jpeg" });
65
+ return toResponseType(buffer, options?.responseType ?? "blob");
40
66
  }
41
- async function getById(id, size = "1024" /* Large */) {
67
+ async function getById(id, options) {
68
+ const size = options?.size ?? "1024" /* Large */;
42
69
  const response = await fetch(`${ApiUrl}/cat/${id}?size=${size}`);
43
70
  if (!response.ok) {
44
71
  throw new Error(`Error fetching cat image: ${response.statusText}`);
45
72
  }
46
73
  const buffer = await response.arrayBuffer();
47
- return new Blob([buffer], { type: "image/jpeg" });
74
+ return toResponseType(buffer, options?.responseType ?? "blob");
48
75
  }
49
76
  async function getInfo(id) {
50
77
  const response = await fetch(`${ApiUrl}/cat/info/${id}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aicats/sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "description": "Official JavaScript SDK for the ai-cats.net API",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",