apexify.js 3.3.6 → 3.3.7

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.
Files changed (128) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  2. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  3. package/.github/dependabot.yml +7 -0
  4. package/.tsbuildinfo +1 -1
  5. package/CODE_OF_CONDUCT.md +128 -0
  6. package/LICENSE +21 -0
  7. package/README.md +27 -21
  8. package/SECURITY.md +20 -0
  9. package/change logs.md +1 -1
  10. package/examples/barchart.txt +0 -2
  11. package/examples/linechart.txt +0 -2
  12. package/lib/ai/ApexAI.ts +116 -85
  13. package/lib/ai/functions/draw.ts +3 -6
  14. package/lib/canvas/ApexPainter.ts +75 -80
  15. package/lib/canvas/utils/bg.ts +7 -4
  16. package/lib/canvas/utils/charts.ts +37 -22
  17. package/lib/canvas/utils/general functions.ts +71 -53
  18. package/lib/canvas/utils/types.ts +14 -7
  19. package/lib/index.ts +6 -6
  20. package/package.json +2 -2
  21. package/dist/ai/ApexAI.d.ts +0 -62
  22. package/dist/ai/ApexAI.d.ts.map +0 -1
  23. package/dist/ai/ApexAI.js +0 -421
  24. package/dist/ai/ApexAI.js.map +0 -1
  25. package/dist/ai/buttons/drawMenu.d.ts +0 -3
  26. package/dist/ai/buttons/drawMenu.d.ts.map +0 -1
  27. package/dist/ai/buttons/drawMenu.js +0 -274
  28. package/dist/ai/buttons/drawMenu.js.map +0 -1
  29. package/dist/ai/buttons/tools.d.ts +0 -3
  30. package/dist/ai/buttons/tools.d.ts.map +0 -1
  31. package/dist/ai/buttons/tools.js +0 -445
  32. package/dist/ai/buttons/tools.js.map +0 -1
  33. package/dist/ai/functions/aivoice.d.ts +0 -1
  34. package/dist/ai/functions/aivoice.d.ts.map +0 -1
  35. package/dist/ai/functions/aivoice.js +0 -2
  36. package/dist/ai/functions/aivoice.js.map +0 -1
  37. package/dist/ai/functions/chunkString.d.ts +0 -2
  38. package/dist/ai/functions/chunkString.d.ts.map +0 -1
  39. package/dist/ai/functions/chunkString.js +0 -8
  40. package/dist/ai/functions/chunkString.js.map +0 -1
  41. package/dist/ai/functions/draw.d.ts +0 -3
  42. package/dist/ai/functions/draw.d.ts.map +0 -1
  43. package/dist/ai/functions/draw.js +0 -387
  44. package/dist/ai/functions/draw.js.map +0 -1
  45. package/dist/ai/functions/generateVoiceResponse.d.ts +0 -3
  46. package/dist/ai/functions/generateVoiceResponse.d.ts.map +0 -1
  47. package/dist/ai/functions/generateVoiceResponse.js +0 -158
  48. package/dist/ai/functions/generateVoiceResponse.js.map +0 -1
  49. package/dist/ai/functions/imageReader.d.ts +0 -3
  50. package/dist/ai/functions/imageReader.d.ts.map +0 -1
  51. package/dist/ai/functions/imageReader.js +0 -23
  52. package/dist/ai/functions/imageReader.js.map +0 -1
  53. package/dist/ai/functions/readFiles.d.ts +0 -3
  54. package/dist/ai/functions/readFiles.d.ts.map +0 -1
  55. package/dist/ai/functions/readFiles.js +0 -62
  56. package/dist/ai/functions/readFiles.js.map +0 -1
  57. package/dist/ai/functions/readImagess.d.ts +0 -2
  58. package/dist/ai/functions/readImagess.d.ts.map +0 -1
  59. package/dist/ai/functions/readImagess.js +0 -48
  60. package/dist/ai/functions/readImagess.js.map +0 -1
  61. package/dist/ai/functions/shouldDrawImage.d.ts +0 -2
  62. package/dist/ai/functions/shouldDrawImage.d.ts.map +0 -1
  63. package/dist/ai/functions/shouldDrawImage.js +0 -12
  64. package/dist/ai/functions/shouldDrawImage.js.map +0 -1
  65. package/dist/ai/functions/typeWriter.d.ts +0 -2
  66. package/dist/ai/functions/typeWriter.d.ts.map +0 -1
  67. package/dist/ai/functions/typeWriter.js +0 -24
  68. package/dist/ai/functions/typeWriter.js.map +0 -1
  69. package/dist/ai/models.d.ts +0 -9
  70. package/dist/ai/models.d.ts.map +0 -1
  71. package/dist/ai/models.js +0 -575
  72. package/dist/ai/models.js.map +0 -1
  73. package/dist/ai/utils.d.ts +0 -11
  74. package/dist/ai/utils.d.ts.map +0 -1
  75. package/dist/ai/utils.js +0 -24
  76. package/dist/ai/utils.js.map +0 -1
  77. package/dist/canvas/ApexPainter.d.ts +0 -38
  78. package/dist/canvas/ApexPainter.d.ts.map +0 -1
  79. package/dist/canvas/ApexPainter.js +0 -439
  80. package/dist/canvas/ApexPainter.js.map +0 -1
  81. package/dist/canvas/utils/bg.d.ts +0 -23
  82. package/dist/canvas/utils/bg.d.ts.map +0 -1
  83. package/dist/canvas/utils/bg.js +0 -74
  84. package/dist/canvas/utils/bg.js.map +0 -1
  85. package/dist/canvas/utils/charts.d.ts +0 -9
  86. package/dist/canvas/utils/charts.d.ts.map +0 -1
  87. package/dist/canvas/utils/charts.js +0 -454
  88. package/dist/canvas/utils/charts.js.map +0 -1
  89. package/dist/canvas/utils/circular.d.ts +0 -10
  90. package/dist/canvas/utils/circular.d.ts.map +0 -1
  91. package/dist/canvas/utils/circular.js +0 -22
  92. package/dist/canvas/utils/circular.js.map +0 -1
  93. package/dist/canvas/utils/customLines.d.ts +0 -2
  94. package/dist/canvas/utils/customLines.d.ts.map +0 -1
  95. package/dist/canvas/utils/customLines.js +0 -50
  96. package/dist/canvas/utils/customLines.js.map +0 -1
  97. package/dist/canvas/utils/general functions.d.ts +0 -21
  98. package/dist/canvas/utils/general functions.d.ts.map +0 -1
  99. package/dist/canvas/utils/general functions.js +0 -391
  100. package/dist/canvas/utils/general functions.js.map +0 -1
  101. package/dist/canvas/utils/imageProperties.d.ts +0 -60
  102. package/dist/canvas/utils/imageProperties.d.ts.map +0 -1
  103. package/dist/canvas/utils/imageProperties.js +0 -331
  104. package/dist/canvas/utils/imageProperties.js.map +0 -1
  105. package/dist/canvas/utils/radius.d.ts +0 -10
  106. package/dist/canvas/utils/radius.d.ts.map +0 -1
  107. package/dist/canvas/utils/radius.js +0 -31
  108. package/dist/canvas/utils/radius.js.map +0 -1
  109. package/dist/canvas/utils/textProperties.d.ts +0 -18
  110. package/dist/canvas/utils/textProperties.d.ts.map +0 -1
  111. package/dist/canvas/utils/textProperties.js +0 -67
  112. package/dist/canvas/utils/textProperties.js.map +0 -1
  113. package/dist/canvas/utils/types.d.ts +0 -414
  114. package/dist/canvas/utils/types.d.ts.map +0 -1
  115. package/dist/canvas/utils/types.js +0 -5
  116. package/dist/canvas/utils/types.js.map +0 -1
  117. package/dist/canvas/utils/utils.d.ts +0 -20
  118. package/dist/canvas/utils/utils.d.ts.map +0 -1
  119. package/dist/canvas/utils/utils.js +0 -48
  120. package/dist/canvas/utils/utils.js.map +0 -1
  121. package/dist/index.d.ts +0 -11
  122. package/dist/index.d.ts.map +0 -1
  123. package/dist/index.js +0 -65
  124. package/dist/index.js.map +0 -1
  125. package/dist/utils.d.ts +0 -12
  126. package/dist/utils.d.ts.map +0 -1
  127. package/dist/utils.js +0 -12
  128. package/dist/utils.js.map +0 -1
package/SECURITY.md ADDED
@@ -0,0 +1,20 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ Use this section to tell people about which versions of your project are
6
+ currently being supported with security updates.
7
+
8
+ | Version | Supported |
9
+ | ------- | ------------------ |
10
+ | >v1.x.x | :white_check_mark: |
11
+ | >v2.x.x | :white_check_mark: |
12
+ | >v3.3.x | :white_check_mark: |
13
+
14
+ ## Reporting a Vulnerability
15
+
16
+ Use this section to tell people how to report a vulnerability.
17
+
18
+ Tell them where to go, how often they can expect to get an update on a
19
+ reported vulnerability, what to expect if the vulnerability is accepted or
20
+ declined, etc.
package/change logs.md CHANGED
@@ -5,7 +5,7 @@
5
5
  ### General Chnages
6
6
 
7
7
  - Documetation moved to [Apexify.js](http://apexify.jedi-studio.com)
8
-
8
+ - Fixed minor bugs
9
9
  ### ApexAI Changes
10
10
 
11
11
  - Added custom nsfw key words filter.
@@ -14,7 +14,6 @@ const data = {
14
14
  },
15
15
  bg: {
16
16
  image: 'https://png.pngtree.com/thumb_back/fh260/background/20211118/pngtree-financial-technology-data-graph-image_908921.jpg',
17
- baseDir: __dirname,
18
17
  bgColor: 'darkgray'
19
18
  },
20
19
  grid: {
@@ -58,7 +57,6 @@ const data = {
58
57
  'blue': 'Label 3',
59
58
  'yellow': 'Lable 4'
60
59
  },
61
- keyColor: '',
62
60
  xTitle: 'X Axis Title',
63
61
  yTitle: 'Y Axis Title',
64
62
  labelStyle: {
@@ -79,13 +79,11 @@ const data = {
79
79
  lineWidth: 3,
80
80
  },
81
81
  canvas: {
82
- bgColor: 'black',
83
82
  fontColor: 'white',
84
83
  fontSize: 16,
85
84
  width: 800,
86
85
  height: 600,
87
86
  image: 'image url or path if u dont want a bg color',
88
- baseDir: __dirname // need to be added if u using a local path image
89
87
  }
90
88
  }
91
89
  };
package/lib/ai/ApexAI.ts CHANGED
@@ -10,12 +10,11 @@ import {
10
10
  typeWriter,
11
11
  readImage,
12
12
  } from "./utils";
13
- import axios from "axios";
14
13
  import {
15
14
  ModalBuilder,
16
15
  TextInputBuilder,
17
16
  TextInputStyle,
18
- ActionRowBuilder
17
+ ActionRowBuilder
19
18
  } from "discord.js";
20
19
  import { filters } from "./buttons/tools";
21
20
  import { imageTools } from "./buttons/drawMenu";
@@ -406,7 +405,6 @@ export async function ApexAI (message: any, aiOptions: Options) {
406
405
  await typeWriter(message.channel, part, speed, delay);
407
406
  }
408
407
  }
409
-
410
408
  } else {
411
409
  if (response.length <= 2000) {
412
410
  await message.reply({
@@ -479,106 +477,139 @@ export async function ApexAI (message: any, aiOptions: Options) {
479
477
  }
480
478
 
481
479
  export async function gemmaAi_4(prompt: string) {
482
- try {
483
- const response = await axios.post('https://api-inference.huggingface.co/models/google/gemma-7b-it', { inputs: prompt }, {
484
- headers: { 'Authorization': `Bearer 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` }
485
- });
486
- return response.data[0].generated_text;
487
- } catch (error: any) {
488
- console.error('Error fetching response:', error.message);
489
- return null;
480
+ try {
481
+ const response = await fetch('https://api-inference.huggingface.co/models/google/gemma-7b-it', {
482
+ method: 'POST',
483
+ headers: {
484
+ 'Content-Type': 'application/json',
485
+ 'Authorization': 'Bearer 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'
486
+ },
487
+ body: JSON.stringify({ inputs: prompt })
488
+ });
489
+
490
+ if (!response.ok) {
491
+ throw new Error('Network response was not ok');
490
492
  }
493
+
494
+ const responseData = await response.json();
495
+ return responseData[0].generated_text;
496
+ } catch (error: any) {
497
+ console.error('Error fetching response:', error.message);
498
+ return null;
499
+ }
491
500
  }
492
501
 
493
502
  export async function gemmaAi_3(prompt: string) {
494
- try {
495
- const response = await axios.post('https://api-inference.huggingface.co/models/google/gemma-2b-it', { inputs: prompt }, {
496
- headers: { 'Authorization': `Bearer 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` }
497
- });
498
- return response.data[0].generated_text;
499
- } catch (error: any) {
500
- console.error('Error fetching response:', error.message);
501
- return null;
502
- }
503
- }
503
+ try {
504
+ const response = await fetch('https://api-inference.huggingface.co/models/google/gemma-2b-it', {
505
+ method: 'POST',
506
+ headers: {
507
+ 'Content-Type': 'application/json',
508
+ 'Authorization': 'Bearer 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'
509
+ },
510
+ body: JSON.stringify({ inputs: prompt })
511
+ });
512
+
513
+ if (!response.ok) {
514
+ throw new Error('Network response was not ok');
515
+ }
504
516
 
517
+ const responseData = await response.json();
518
+ return responseData[0].generated_text;
519
+ } catch (error: any) {
520
+ console.error('Error fetching response:', error.message);
521
+ return null;
522
+ }
523
+ }
505
524
  export async function apexai(prompt: string) {
506
- try {
525
+ try {
507
526
  const messages = [
508
- {"role": "user", "content": `${prompt}`}
527
+ {"role": "user", "content": `${prompt}`}
509
528
  ];
510
- const formattedMessages = messages.map(message => `[${message.role}] ${message.content}`).join('\n');
511
-
512
- const response = await axios.post(`https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1`, {
513
- inputs: formattedMessages
514
- }, {
515
- headers: {
516
- 'Authorization': `Bearer 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`,
517
- 'Content-Type': 'application/json'
518
- }
519
- });
520
-
521
- const generatedText = response.data[0].generated_text;
529
+ const formattedMessages = messages.map(message => `[${message.role}] ${message.content}`).join('\n');
530
+
531
+ const response = await fetch('https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1', {
532
+ method: 'POST',
533
+ headers: {
534
+ 'Authorization': 'Bearer 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',
535
+ 'Content-Type': 'application/json'
536
+ },
537
+ body: JSON.stringify({ inputs: formattedMessages })
538
+ });
522
539
 
523
- const lines = generatedText.split('\n').slice(1);
540
+ if (!response.ok) {
541
+ throw new Error('Network response was not ok');
542
+ }
524
543
 
525
- const output = lines.join('\n');
544
+ const responseData = await response.json();
545
+ const generatedText = responseData[0].generated_text;
546
+ const lines = generatedText.split('\n').slice(1);
547
+ const output = lines.join('\n');
526
548
 
527
- return output
528
- } catch (error: any) {
529
- console.error('Error:', error.response.data);
530
- return 'Please wait i am on cooldown.'
531
- }
549
+ return output;
550
+ } catch (error: any) {
551
+ console.error('Error:', error.message);
552
+ return 'Please wait i am on cooldown.';
553
+ }
532
554
  }
533
555
 
534
556
  export async function starChat(prompt: string) {
535
- const messages = [{"role":"user","content": `${prompt}`}]
536
-
537
- try {
538
- const response = await axios.post('https://api-inference.huggingface.co/models/HuggingFaceH4/starchat2-15b-v0.1', {
539
- inputs: JSON.stringify(messages),
540
- }, {
541
- headers: {
542
- 'Authorization': `Bearer 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`,
543
- },
544
- });
557
+ const messages = [{"role":"user","content": `${prompt}`}];
558
+
559
+ try {
560
+ const response = await fetch('https://api-inference.huggingface.co/models/HuggingFaceH4/starchat2-15b-v0.1', {
561
+ method: 'POST',
562
+ headers: {
563
+ 'Authorization': 'Bearer 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',
564
+ 'Content-Type': 'application/json'
565
+ },
566
+ body: JSON.stringify({ inputs: messages })
567
+ });
545
568
 
546
- const chatbotReply = response.data[0];
547
- const chatbotResponseText = chatbotReply.generated_text.replace(/^.*?\n.*?\n/, '');
548
- const chatbotResponseArray = JSON.parse(chatbotResponseText);
549
- const chatbotResponseString = chatbotResponseArray.join(' ');
569
+ if (!response.ok) {
570
+ throw new Error('Network response was not ok');
571
+ }
550
572
 
551
- return chatbotResponseString;
552
- } catch (error: any) {
553
- console.error('Error fetching response:', error.message);
554
- return null;
555
- }
573
+ const responseData = await response.json();
574
+ const chatbotReply = responseData[0];
575
+ const chatbotResponseText = chatbotReply.generated_text.replace(/^.*?\n.*?\n/, '');
576
+ const chatbotResponseArray = JSON.parse(chatbotResponseText);
577
+ const chatbotResponseString = chatbotResponseArray.join(' ');
578
+
579
+ return chatbotResponseString;
580
+ } catch (error: any) {
581
+ console.error('Error:', error.message);
582
+ return null;
583
+ }
556
584
  }
557
585
 
558
586
  export async function zephyr_beta(prompt: string) {
587
+ const messages = [{"role":"user","content": `${prompt}` }];
588
+
589
+ try {
590
+ const response = await fetch('https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta', {
591
+ method: 'POST',
592
+ headers: {
593
+ 'Authorization': 'Bearer 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',
594
+ 'Content-Type': 'application/json'
595
+ },
596
+ body: JSON.stringify({ inputs: messages })
597
+ });
559
598
 
560
- const messages = [{"role":"user","content": `${prompt}` }]
561
- try {
562
- const response = await axios.post('https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta', {
563
- inputs: JSON.stringify(messages),
564
- }, {
565
- headers: {
566
- 'Authorization': `Bearer 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`,
567
- },
568
- });
569
-
570
- const chatbotReply = response.data[0];
571
-
572
- const textParts = chatbotReply.generated_text.split('\n');
573
-
574
- const secondArrayString = textParts[2];
575
- const chatbotResponseArray = JSON.parse(secondArrayString);
576
-
577
- const chatbotResponseString = chatbotResponseArray.map((obj: any) => obj.content).join(' ');
599
+ if (!response.ok) {
600
+ throw new Error('Network response was not ok');
601
+ }
578
602
 
579
- return chatbotResponseString;
580
- } catch (error: any) {
581
- console.error('Error fetching response:', error.message);
582
- return null;
583
- }
584
- }
603
+ const responseData = await response.json();
604
+ const chatbotReply = responseData[0];
605
+ const textParts = chatbotReply.generated_text.split('\n');
606
+ const secondArrayString = textParts[2];
607
+ const chatbotResponseArray = JSON.parse(secondArrayString);
608
+ const chatbotResponseString = chatbotResponseArray.map((obj: any) => obj.content).join(' ');
609
+
610
+ return chatbotResponseString;
611
+ } catch (error: any) {
612
+ console.error('Error:', error.message);
613
+ return null;
614
+ }
615
+ }
@@ -88,8 +88,8 @@ async function aiImagine(
88
88
  nsfw: boolean,
89
89
  nsfwKeyWords: string[],
90
90
  ) {
91
- const maxRetryAttempts = 4;
92
- const retryInterval = 5000;
91
+ const maxRetryAttempts = 3;
92
+ const retryInterval = 10000;
93
93
  let response: any;
94
94
  async function retry(fn: any, retriesLeft = maxRetryAttempts) {
95
95
  try {
@@ -98,7 +98,6 @@ async function aiImagine(
98
98
  if (retriesLeft === 0) {
99
99
  throw error;
100
100
  }
101
-
102
101
  await new Promise((resolve) => setTimeout(resolve, retryInterval));
103
102
  return retry(fn, retriesLeft - 1);
104
103
  }
@@ -421,9 +420,7 @@ async function attemptImageCaptioning(imageUrl: string) {
421
420
  try {
422
421
  return await fetchData();
423
422
  } catch (error: any) {
424
- console.error(
425
- `Error fetching data (Retry ${retryCount + 1}): ${error.message}`,
426
- );
423
+
427
424
  retryCount++;
428
425
  }
429
426
  }
@@ -1,5 +1,4 @@
1
1
  import { createCanvas, loadImage, GlobalFonts } from "@napi-rs/canvas";
2
- import axios, { responseEncoding } from "axios";
3
2
  import path from 'path';
4
3
  import GIFEncoder from 'gifencoder';
5
4
  import { PassThrough, Writable} from 'stream'
@@ -11,55 +10,57 @@ import { CanvasConfig, TextObject, ImageProperties, ImageObject, GIFOptions, GIF
11
10
  import { } from "./utils/general functions";
12
11
 
13
12
  export class ApexPainter {
14
- async createCanvas(canvas: CanvasConfig): Promise<Buffer> {
15
- let canvasWidth: number = canvas.width || 500;
16
- let canvasHeight: number = canvas.height || 500;
17
- let borderRadius: number | string = canvas.borderRadius || 0;
18
13
 
19
- if (canvas.customBg) {
20
- try {
21
- const response = await axios.get(canvas.customBg, { responseType: 'arraybuffer' });
22
- const imageBuffer = Buffer.from(response.data, 'binary');
23
- const image = await loadImage(imageBuffer);
24
-
25
- canvasWidth = image.width;
26
- canvasHeight = image.height;
27
- } catch (error) {
28
- console.error('Error loading custom background image:', error);
14
+ async createCanvas(canvas: CanvasConfig): Promise<Buffer> {
15
+ let canvasWidth: number = canvas.width || 500;
16
+ let canvasHeight: number = canvas.height || 500;
17
+ let borderRadius: number | string = canvas.borderRadius || 0;
18
+
19
+ if (canvas.customBg) {
20
+ try {
21
+ const response = await fetch(canvas.customBg);
22
+ if (!response.ok) {
23
+ throw new Error("Failed to fetch background image.");
29
24
  }
25
+ const buffer = await response.arrayBuffer();
26
+ const image = await loadImage(Buffer.from(buffer));
27
+ canvasWidth = image.width;
28
+ canvasHeight = image.height;
29
+ } catch (error) {
30
+ console.error('Error loading custom background image:', error);
30
31
  }
32
+ }
31
33
 
32
-
33
-
34
- const canvasInstance = createCanvas(canvasWidth, canvasHeight);
35
- const ctx: any = canvasInstance.getContext('2d');
34
+ const canvasInstance = createCanvas(canvasWidth, canvasHeight);
35
+ const ctx: any = canvasInstance.getContext('2d');
36
36
 
37
- if (!ctx) {
38
- throw new Error('Unable to get 2D rendering context from canvas');
39
- }
40
-
41
- applyRotation(ctx, canvas.rotation || 0, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
37
+ if (!ctx) {
38
+ throw new Error('Unable to get 2D rendering context from canvas');
39
+ }
42
40
 
43
- applyShadow(ctx, canvas.shadow, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
41
+ applyRotation(ctx, canvas.rotation || 0, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
44
42
 
45
- if (typeof borderRadius === 'string') {
46
- circularBorder(ctx, canvasWidth, canvasHeight);
47
- } else if (typeof borderRadius === 'number') {
48
- radiusBorder(ctx, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight, borderRadius);
49
- }
43
+ applyShadow(ctx, canvas.shadow, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
50
44
 
51
- if (canvas.customBg) {
52
- await customBackground(ctx, canvas);
53
- } else if (canvas.gradientBg) {
54
- await drawBackgroundGradient(ctx, canvas);
55
- } else {
56
- await drawBackgroundColor(ctx, canvas);
57
- }
58
- applyStroke(ctx, canvas.stroke, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
45
+ if (typeof borderRadius === 'string') {
46
+ circularBorder(ctx, canvasWidth, canvasHeight);
47
+ } else if (typeof borderRadius === 'number') {
48
+ radiusBorder(ctx, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight, borderRadius);
49
+ }
59
50
 
60
- return canvasInstance.toBuffer('image/png');
51
+ if (canvas.customBg) {
52
+ await customBackground(ctx, canvas);
53
+ } else if (canvas.gradientBg) {
54
+ await drawBackgroundGradient(ctx, canvas);
55
+ } else {
56
+ await drawBackgroundColor(ctx, canvas);
61
57
  }
62
58
 
59
+ applyStroke(ctx, canvas.stroke, canvas.x || 0, canvas.y || 0, canvasWidth, canvasHeight);
60
+
61
+ return canvasInstance.toBuffer('image/png');
62
+ }
63
+
63
64
  async createImage(images: ImageProperties[], canvasBuffer: Buffer): Promise<Buffer> {
64
65
 
65
66
  if (!canvasBuffer) {
@@ -517,51 +518,45 @@ export class ApexPainter {
517
518
  }
518
519
 
519
520
  try {
520
-
521
- let img: any;
522
- const shapeNames = ['circle', 'square', 'triangle', 'pentagon', 'hexagon', 'heptagon', 'octagon', 'star', 'kite'];
523
-
524
- const isShape = shapeNames.includes(source.toLowerCase());
525
-
526
- if (source.startsWith('http')) {
527
- const response = await axios.get(source, { responseType: 'arraybuffer' });
528
- const imageBuffer = Buffer.from(response.data, 'binary');
529
- img = await loadImage(imageBuffer);
530
- } else if (isShape) {
531
- drawShape(ctx, { source, x, y, width, height, rotation, borderRadius, stroke, shadow, isFilled, color, gradient });
532
- } else {
533
- const imagePath = path.join(process.cwd(), source);
534
- try {
535
- img = await loadImage(imagePath);
536
- } catch (e: any) {
537
- throw new Error(`Error loading image: ${e.message}`);
521
+ let img: any;
522
+ const shapeNames = ['circle', 'square', 'triangle', 'pentagon', 'hexagon', 'heptagon', 'octagon', 'star', 'kite'];
523
+ const isShape = shapeNames.includes(source.toLowerCase());
524
+
525
+ if (source.startsWith('http')) {
526
+ const response = await fetch(source);
527
+ if (!response.ok) {
528
+ throw new Error("Failed to fetch image.");
529
+ }
530
+ const buffer = await response.arrayBuffer();
531
+ img = await loadImage(Buffer.from(buffer));
532
+ } else if (isShape) {
533
+ drawShape(ctx, { source, x, y, width, height, rotation, borderRadius, stroke, shadow, isFilled, color, gradient });
534
+ } else {
535
+ const imagePath = path.join(process.cwd(), source);
536
+ try {
537
+ img = await loadImage(imagePath);
538
+ } catch (e: any) {
539
+ throw new Error(`Error loading image: ${e.message}`);
540
+ }
538
541
  }
539
- }
540
-
541
- if (img !== undefined) {
542
-
543
- ctx.save();
544
-
545
- applyRotation(ctx, rotation || 0, x, y, width, height);
546
-
547
- applyShadow(ctx, shadow, x, y, width, height);
548
542
 
549
- imageRadius(ctx, img, x, y, width, height, borderRadius || 0);
550
-
551
- applyStroke(ctx, stroke, x, y, width, height);
552
-
553
- ctx.restore();
554
- } else {
555
- if (!isShape) {
556
- throw new Error('Invalid image source or shape name');
543
+ if (img !== undefined) {
544
+ ctx.save();
545
+ applyRotation(ctx, rotation || 0, x, y, width, height);
546
+ applyShadow(ctx, shadow, x, y, width, height);
547
+ imageRadius(ctx, img, x, y, width, height, borderRadius || 0);
548
+ applyStroke(ctx, stroke, x, y, width, height);
549
+ ctx.restore();
550
+ } else {
551
+ if (!isShape) {
552
+ throw new Error('Invalid image source or shape name');
553
+ }
557
554
  }
558
- }
559
-
560
- } catch (e: any) {
561
- throw new Error(e.message)
562
- }
555
+ } catch (error: any) {
556
+ throw new Error(error.message);
557
+ }
558
+
563
559
  }
564
-
565
560
  public validHex(hexColor: string): any {
566
561
  const hexPattern = /^#[0-9a-fA-F]{6}$/;
567
562
  if (!hexPattern.test(hexColor)) {
@@ -1,6 +1,5 @@
1
1
  import { loadImage } from "@napi-rs/canvas";
2
2
  import { CanvasConfig } from './types';
3
- import axios from 'axios';
4
3
  import fs from 'fs';
5
4
  import path from 'path';
6
5
 
@@ -58,8 +57,12 @@ export async function customBackground(ctx: any, canvas: CanvasConfig): Promise<
58
57
  let imagePath: string;
59
58
 
60
59
  if (canvas.customBg.startsWith('http')) {
61
- const response = await axios.get(canvas.customBg, { responseType: 'arraybuffer' });
62
- imageBuffer = Buffer.from(response.data, 'binary');
60
+ const response = await fetch(canvas.customBg);
61
+ if (!response.ok) {
62
+ throw new Error("Failed to fetch custom background image.");
63
+ }
64
+ const buffer = await response.arrayBuffer();
65
+ imageBuffer = Buffer.from(buffer);
63
66
  } else {
64
67
  imagePath = path.join(process.cwd(), canvas.customBg);
65
68
  imageBuffer = fs.readFileSync(imagePath);
@@ -72,7 +75,7 @@ export async function customBackground(ctx: any, canvas: CanvasConfig): Promise<
72
75
 
73
76
  ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
74
77
 
75
- } catch (error) {
78
+ } catch (error: any) {
76
79
  console.error('Error loading custom background image:', error);
77
80
  }
78
81
  }