diff --git a/README.md b/README.md index ae067cb..bbe943c 100644 --- a/README.md +++ b/README.md @@ -29,24 +29,55 @@ const dalle = new Dalle({ apiKey: "sess-xxxxxxxxxxxxxxxxxxxxxxxxxxxx" }); // Create an async function const getDalle2Images = async (caption) => { - const generatedImages = await dalle.generate(caption); + // Call the Dall-e 2 API + const response = await dalle.generate(caption); - return generatedImages; -} + // If Dall-e 2 couldn't generate images from the given caption + if (!response) { + console.error( + "Dall-e 2 couldn't generate images based upon the given caption." + ); + return null; + } + + // Get the image array from the response object + const { data } = response; -// Call the function with top level await -const { data } = await getDalle2Images("Man in a suit riding a horse during the medieval times"); + // Return the image array + return data; +}; +``` +## Calling the function using top level await +```js +// Using top level await +const data = await getDalle2Images("Man in a suit riding a horse during the medieval times"); -// Log the response +// If the image array is empty for some reason +if (!data) { + console.error("Something has gone horribly wrong..."); +} + +// Log the image array console.log(data); +``` -// Or use .then syntax -getDalle2Images("Man in a suit riding a horse during the medieval times").then( - ({ data }) => { +## Calling the function using .then syntax +```js +getDalle2Images("Man in a suit riding a horse during the medieval times") + .then((data) => { + + // If the image array is empty for some reason + if (!data) { + console.error("Something has gone horribly wrong..."); + return null; + } + + // Log the image array console.log(data); } ); ``` + ### Output ``` [ diff --git a/package.json b/package.json index a2127ce..6de69bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-dalle2", - "version": "1.0.5", + "version": "1.1.0", "description": "Nodejs library for interacting with OpenAI's Dalle-2 AI", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -19,11 +19,27 @@ "type": "git", "url": "https://github.com/JoshHyde9/node-dalle2.git" }, - "keywords": ["nodejs", "javascript", "typescript", "types", "openai", "openai-api", "dall-e", "dalle", "dalle2", "dalle-nodejs", "dalle2-api", "dall-e-2-api" ,"node-dalle2", "api", "node"], + "keywords": [ + "nodejs", + "javascript", + "typescript", + "types", + "openai", + "openai-api", + "dall-e", + "dalle", + "dalle2", + "dalle-nodejs", + "dalle2-api", + "dall-e-2-api", + "node-dalle2", + "api", + "node" + ], "author": "Josh Hyde", "license": "ISC", "devDependencies": { "@types/node": "^18.6.2", "typescript": "^4.7.4" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index bc3879c..4b0c7aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,20 +25,22 @@ export interface Task { }; } +export interface IData { + id: string; + object: string; + created: number; + generation_type: string; + generation: { image_path: string }; + task_id: string; + prompt_id: string; + is_public: boolean; +} + +export type ImageData = IData[]; + export interface ImageGenerations { object: string; - data: [ - { - id: string; - object: string; - created: number; - generation_type: string; - generation: { image_path: string }; - task_id: string; - prompt_id: string; - is_public: boolean; - } - ]; + data: ImageData; } export class Dalle { @@ -55,7 +57,7 @@ export class Dalle { prompt: { caption: prompt, batch_size: 4 }, }; - return new Promise(async (resolve, _) => { + return new Promise(async (resolve, reject) => { const response = await fetch(this.url, { method: "POST", headers: { @@ -67,7 +69,9 @@ export class Dalle { if (!response.ok) { console.error(response); - return; + return reject( + "Dall-e 2 couldn't generate images based upon your caption." + ); } const data: Task = await response.json(); @@ -84,7 +88,9 @@ export class Dalle { if (!response.ok) { console.error(response); - return; + return reject( + "Dall-e 2 couldn't generate images based upon your caption." + ); } const data: Task = await response.json(); diff --git a/tsconfig.json b/tsconfig.json index 6d4cf84..d2cf651 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,13 +1,13 @@ { "compilerOptions": { - "target": "es2017", - "module": "commonjs", + "target": "ES2022", + "module": "ES2022", "lib": ["dom", "es6", "es2017", "esnext.asynciterable"], "skipLibCheck": true, - "sourceMap": true, + "sourceMap": false, "outDir": "./dist", - "declaration": true, "moduleResolution": "node", + "declaration": true, "removeComments": true, "noImplicitAny": true, "strictNullChecks": true,