orshot 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.
@@ -0,0 +1,23 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
+
4
+ name: Publish Package to npmjs
5
+ on:
6
+ release:
7
+ types: [published]
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+ permissions:
12
+ contents: read
13
+ id-token: write
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: actions/setup-node@v4
17
+ with:
18
+ node-version: '20.x'
19
+ registry-url: 'https://registry.npmjs.org'
20
+ - run: npm ci
21
+ - run: npm publish --provenance --access public
22
+ env:
23
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/README.md ADDED
@@ -0,0 +1,174 @@
1
+ # Orshot Node.js API SDK
2
+
3
+ View on npmjs: https://www.npmjs.com/package/orshot
4
+
5
+ ## Installation
6
+
7
+ Using `npm`
8
+ ```
9
+ npm install --save orshot
10
+ ```
11
+
12
+ Using `yarn`
13
+ ```
14
+ yarn add orshot
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ If you don't have your API key, get one from [orshot.com](https://orshot.com)
20
+
21
+ ### Import
22
+
23
+ ```
24
+ const { Orshot } = require('orshot');
25
+ ```
26
+
27
+ With `ES6`
28
+
29
+ ```
30
+ import { Orshot } from "orshot";
31
+ ```
32
+
33
+ ### Initialise
34
+
35
+ ```js
36
+ const orshot = new Orshot("Your API key");
37
+ ```
38
+
39
+ ### Generate image
40
+
41
+ ```js
42
+ const response = await Orshot.renderFromTemplate({templateId, modifications, responseType: "base64", responseFormat: "png"});
43
+ console.log(response);
44
+ ```
45
+
46
+ ## Example
47
+
48
+ ### `Base64` response format
49
+
50
+ ```js
51
+ import { Orshot } from "orshot";
52
+
53
+ const orshot = new Orshot("os-ha2jdus1cbz1dpt4mktgjyvx");
54
+
55
+ let templateId = "open-graph-image-1";
56
+ let modifications = {
57
+ title: "Orshot",
58
+ description: "Create Visuals and Automate Image Generation",
59
+ textColor: "",
60
+ backgroundImageUrl: "",
61
+ backgroundColor: ""
62
+ }
63
+
64
+ const response = await orshot.renderFromTemplate({templateId, modifications, responseType: "base64", responseFormat: "png"});
65
+ console.log(response);
66
+ ```
67
+
68
+ API Response
69
+ ```
70
+ {
71
+ data: {
72
+ content: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABLAAAAJ2CAYAAABPQHtcAAAAAXNSR0IArs4c6QAAIABJREFUeJzs3XmYJXdZL/Bvna37dM90FghLCBAQkC1BCBAMShLFBJAgKnofroBeFUUF5LrhiihXcV8BQRYVUUAlIewIGPbFmLCFLWwCYZEtzPR+trp/TM/......',
73
+ format: 'png',
74
+ type: 'base64',
75
+ responseTime: 3375.72
76
+ }
77
+ }
78
+ ```
79
+
80
+ ### `URL` response format
81
+
82
+ ```js
83
+ import { Orshot } from "orshot";
84
+
85
+ const orshot = new Orshot("os-ha2jdus1cbz1dpt4mktgjyvx");
86
+
87
+ let templateId = "open-graph-image-1";
88
+ let modifications = {
89
+ title: "Orshot",
90
+ description: "Create Visuals and Automate Image Generation",
91
+ textColor: "",
92
+ backgroundImageUrl: "",
93
+ backgroundColor: ""
94
+ }
95
+
96
+ const response = await orshot.renderFromTemplate({templateId, modifications, responseType: "url", responseFormat: "png"});
97
+ console.log(response);
98
+ ```
99
+
100
+ API Response
101
+ ```
102
+ {
103
+ data: {
104
+ content: 'https://storage.orshot.com/10631481-fd26-44ff-9a61-f52cdf1b8e62/images/r1wCliKXC2B.png',
105
+ type: 'url',
106
+ format: 'png',
107
+ responseTime: 3550.43
108
+ }
109
+ }
110
+ ```
111
+
112
+ ### `Binary` response format
113
+
114
+ ```js
115
+ import { Orshot } from "orshot";
116
+ import { createWriteStream } from 'fs';
117
+
118
+ const orshot = new Orshot("os-he2jdus1cbz1dpt4mktgjyvx");
119
+
120
+ let templateId = "open-graph-image-1";
121
+ let modifications = {
122
+ title: "Orshot",
123
+ description: "Create Visuals and Automate Image Generation",
124
+ textColor: "",
125
+ backgroundImageUrl: "",
126
+ backgroundColor: ""
127
+ }
128
+
129
+ const response = await orshot.renderFromTemplate({templateId, modifications, responseType: "binary", responseFormat: "png"});
130
+ const arrayBuffer = await response.arrayBuffer();
131
+ const buffer = Buffer.from(arrayBuffer);
132
+
133
+ createWriteStream("og.png").write(buffer);
134
+ ```
135
+
136
+ This example writes the binary image to the file `og.png` in the current directory.
137
+
138
+ ## renderFromTemplate
139
+
140
+ Use this function to render an image/pdf. Render template takes in 4 options passed as an object
141
+
142
+ ```
143
+ {
144
+ templateId,
145
+ modifications,
146
+ responseType,
147
+ responseFormat
148
+ }
149
+ ```
150
+
151
+ | key | required | description |
152
+ |----------|----------|-------------|
153
+ | `templateId` | Yes | ID of the template (`open-graph-image-1`, `tweet-image-1`, `beautify-screenshot-1`, ...) |
154
+ | `modifications` | Yes | Modifications for the selected template. |
155
+ | `responseType` | No | `base64`, `binary`, `url` (Defaults to `base64`). |
156
+ | `responseFormat` | No | `png`, `webp`, `pdf`, `jpg`, `jpeg` (Defaults to `png`). |
157
+
158
+ For available templates and their modifications refer [Orshot Templates Page](https://orshot.com/templates)
159
+
160
+ ## Local development and testing
161
+
162
+ Run these from the project
163
+
164
+ `npm run build`
165
+
166
+ `npm run link`
167
+
168
+ Create a test directory and add `index.js` file
169
+
170
+ Write simple code to generate an image from a template.
171
+
172
+ From the `test` directory, run `npm link orshot`
173
+
174
+ You can now run `node index.js` to test if the sdk code works as expected.
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "orshot",
3
+ "version": "0.1.0",
4
+ "description": "Orshot API SDK for node.js",
5
+ "homepage": "https://github.com/rishimohan/orshot-nodejs-sdk#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/rishimohan/orshot-nodejs-sdk/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/rishimohan/orshot-nodejs-sdk.git"
12
+ },
13
+ "license": "ISC",
14
+ "author": "Rishi Mohan <iamrishi.ms@gmail.com>",
15
+ "main": "dist/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "scripts": {
18
+ "build": "tsc"
19
+ },
20
+ "devDependencies": {
21
+ "typescript": "^5.8.2"
22
+ }
23
+ }
@@ -0,0 +1,5 @@
1
+ export const ORSHOT_SOURCE = "orshot-node-sdk";
2
+ export const ORSHOT_API_BASE_URL = "https://api.orshot.com";
3
+ export const ORSHOT_API_VERSION = "v1";
4
+ export const DEFAULT_RESPONSE_TYPE = "base64";
5
+ export const DEFAULT_RESPONSE_FORMAT = "png";
package/src/index.ts ADDED
@@ -0,0 +1,72 @@
1
+ import { TemplateRenderOptions } from "./types";
2
+ import { DEFAULT_RESPONSE_TYPE, DEFAULT_RESPONSE_FORMAT, ORSHOT_API_BASE_URL, ORSHOT_API_VERSION, ORSHOT_SOURCE } from "./constants";
3
+
4
+ export class Orshot {
5
+ private readonly apiKey: string;
6
+
7
+ constructor(apiKey: string) {
8
+ if (!apiKey) {
9
+ throw new Error("API Key is required.")
10
+ }
11
+ this.apiKey = apiKey;
12
+ }
13
+
14
+ public getBaseUrl(version?: string) {
15
+ const baseUrl = ORSHOT_API_BASE_URL;
16
+
17
+ let apiVersion = ORSHOT_API_VERSION;
18
+
19
+ if (version) {
20
+ apiVersion = version;
21
+ }
22
+
23
+ return `${baseUrl}/${apiVersion}`;
24
+ }
25
+
26
+ public getHeaders() {
27
+ return {
28
+ 'Content-Type': 'application/json',
29
+ 'Authorization': `Bearer ${this.apiKey}`
30
+ };
31
+ }
32
+
33
+ public async renderFromTemplate(renderOptions: TemplateRenderOptions) {
34
+ let { templateId, modifications, responseType, responseFormat } = renderOptions;
35
+
36
+ if (!responseType) {
37
+ responseType = DEFAULT_RESPONSE_TYPE;
38
+ }
39
+
40
+ if (!responseFormat) {
41
+ responseFormat = DEFAULT_RESPONSE_FORMAT;
42
+ }
43
+
44
+ let endpoint = `${this.getBaseUrl()}/generate/images/${templateId}`;
45
+
46
+ const response = await fetch(endpoint, {
47
+ method: "POST",
48
+ headers: this.getHeaders(),
49
+ body: JSON.stringify({
50
+ response: {
51
+ type: responseType,
52
+ format: responseFormat
53
+ },
54
+ modifications: modifications,
55
+ source: ORSHOT_SOURCE
56
+ }),
57
+ });
58
+
59
+ if (!response.ok) {
60
+ throw new Error("Failed to fetch image: " + response.status);
61
+ }
62
+
63
+ if (responseType === "base64" || responseType === "url") {
64
+ const jsonData = await response.json();
65
+ return jsonData;
66
+ } else {
67
+ return response;
68
+ }
69
+ }
70
+ }
71
+
72
+ export default Orshot;
package/src/types.ts ADDED
@@ -0,0 +1,9 @@
1
+ export type ResponseType = "base64" | "binary" | "url";
2
+ export type ResponseFormat = "png" | "webp" | "pdf" | "jpg" | "jpeg" ;
3
+
4
+ export type TemplateRenderOptions = {
5
+ templateId: string;
6
+ modifications: any;
7
+ responseType?: ResponseType;
8
+ responseFormat?: ResponseFormat;
9
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "esModuleInterop": true,
5
+ "declaration": true,
6
+ "target": "esnext",
7
+ "moduleResolution": "node",
8
+ "sourceMap": true,
9
+ "outDir": "dist"
10
+ },
11
+ "include": [
12
+ "src/**/*"
13
+ ],
14
+ "exclude": [
15
+ "node_modules",
16
+ "dist"
17
+ ]
18
+ }