Skip to content

Commit

Permalink
Added better support for catching errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshHyde9 committed Aug 1, 2022
1 parent 2a692e5 commit 5b04e21
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 31 deletions.
49 changes: 40 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
[
Expand Down
22 changes: 19 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}
}
36 changes: 21 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -55,7 +57,7 @@ export class Dalle {
prompt: { caption: prompt, batch_size: 4 },
};

return new Promise<ImageGenerations>(async (resolve, _) => {
return new Promise<ImageGenerations | null>(async (resolve, reject) => {
const response = await fetch(this.url, {
method: "POST",
headers: {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 5b04e21

Please sign in to comment.