orshot 0.1.0 → 0.1.2

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.
@@ -18,6 +18,7 @@ jobs:
18
18
  node-version: '20.x'
19
19
  registry-url: 'https://registry.npmjs.org'
20
20
  - run: npm ci
21
+ - run: npm run build
21
22
  - run: npm publish --provenance --access public
22
23
  env:
23
24
  NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/README.md CHANGED
@@ -43,6 +43,13 @@ const response = await Orshot.renderFromTemplate({templateId, modifications, res
43
43
  console.log(response);
44
44
  ```
45
45
 
46
+ ### Generate signed URL
47
+
48
+ ```js
49
+ const response = await Orshot.renderFromTemplate({templateId, modifications, expiresAt: 1744276943, renderType: "images", responseFormat: "png"});
50
+ console.log(response);
51
+ ```
52
+
46
53
  ## Example
47
54
 
48
55
  ### `Base64` response format
@@ -135,6 +142,26 @@ createWriteStream("og.png").write(buffer);
135
142
 
136
143
  This example writes the binary image to the file `og.png` in the current directory.
137
144
 
145
+ ### Signed URL
146
+
147
+ ```js
148
+ import { Orshot } from "orshot";
149
+
150
+ const orshot = new Orshot("os-ha2jdus1cbz1dpt4mktgjyvx");
151
+
152
+ let templateId = "open-graph-image-1";
153
+ let modifications = {
154
+ title: "Orshot",
155
+ description: "Create Visuals and Automate Image Generation",
156
+ textColor: "",
157
+ backgroundImageUrl: "",
158
+ backgroundColor: ""
159
+ }
160
+
161
+ const response = await Orshot.renderFromTemplate({templateId, modifications, expiresAt: 1744276943, renderType: "images", responseFormat: "png"});
162
+ console.log(response);
163
+ ```
164
+
138
165
  ## renderFromTemplate
139
166
 
140
167
  Use this function to render an image/pdf. Render template takes in 4 options passed as an object
@@ -157,13 +184,36 @@ Use this function to render an image/pdf. Render template takes in 4 options pas
157
184
 
158
185
  For available templates and their modifications refer [Orshot Templates Page](https://orshot.com/templates)
159
186
 
187
+ ## generateSignedUrl
188
+
189
+ Use this function to generate signed URL.
190
+
191
+ ```
192
+ {
193
+ templateId,
194
+ modifications,
195
+ renderType,
196
+ responseFormat,
197
+ expiresAt
198
+ }
199
+ ```
200
+
201
+ | key | required | description |
202
+ |----------|----------|-------------|
203
+ | `templateId` | Yes | ID of the template (`open-graph-image-1`, `tweet-image-1`, `beautify-screenshot-1`, ...) |
204
+ | `modifications` | Yes | Modifications for the selected template. |
205
+ | `expiresAt` | Yes | Expires at in unix timestamp (Number). |
206
+ | `renderType` | No | `images`, `pdfs` (Defaults to `images`). |
207
+ | `responseFormat` | No | `png`, `webp`, `pdf`, `jpg`, `jpeg` (Defaults to `png`). |
208
+
209
+
160
210
  ## Local development and testing
161
211
 
162
212
  Run these from the project
163
213
 
164
214
  `npm run build`
165
215
 
166
- `npm run link`
216
+ `npm link`
167
217
 
168
218
  Create a test directory and add `index.js` file
169
219
 
@@ -172,3 +222,7 @@ Write simple code to generate an image from a template.
172
222
  From the `test` directory, run `npm link orshot`
173
223
 
174
224
  You can now run `node index.js` to test if the sdk code works as expected.
225
+
226
+ ## Publish package to NPM
227
+
228
+ Create a new release from GitHub. This will trigger a GitHub action which will publish the package to NPM.
@@ -0,0 +1,6 @@
1
+ export declare const ORSHOT_SOURCE = "orshot-node-sdk";
2
+ export declare const ORSHOT_API_BASE_URL = "https://api.orshot.com";
3
+ export declare const ORSHOT_API_VERSION = "v1";
4
+ export declare const DEFAULT_RESPONSE_TYPE = "base64";
5
+ export declare const DEFAULT_RESPONSE_FORMAT = "png";
6
+ export declare const DEFAULT_RENDER_TYPE = "images";
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_RENDER_TYPE = exports.DEFAULT_RESPONSE_FORMAT = exports.DEFAULT_RESPONSE_TYPE = exports.ORSHOT_API_VERSION = exports.ORSHOT_API_BASE_URL = exports.ORSHOT_SOURCE = void 0;
4
+ exports.ORSHOT_SOURCE = "orshot-node-sdk";
5
+ exports.ORSHOT_API_BASE_URL = "https://api.orshot.com";
6
+ exports.ORSHOT_API_VERSION = "v1";
7
+ exports.DEFAULT_RESPONSE_TYPE = "base64";
8
+ exports.DEFAULT_RESPONSE_FORMAT = "png";
9
+ exports.DEFAULT_RENDER_TYPE = "images";
10
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,aAAa,GAAG,iBAAiB,CAAC;AAClC,QAAA,mBAAmB,GAAG,wBAAwB,CAAC;AAC/C,QAAA,kBAAkB,GAAG,IAAI,CAAC;AAC1B,QAAA,qBAAqB,GAAG,QAAQ,CAAC;AACjC,QAAA,uBAAuB,GAAG,KAAK,CAAC;AAChC,QAAA,mBAAmB,GAAG,QAAQ,CAAC"}
@@ -0,0 +1,13 @@
1
+ import { TemplateRenderOptions, SignedUrlOptions } from "./types";
2
+ export declare class Orshot {
3
+ private readonly apiKey;
4
+ constructor(apiKey: string);
5
+ getBaseUrl(version?: string): string;
6
+ getHeaders(): {
7
+ 'Content-Type': string;
8
+ Authorization: string;
9
+ };
10
+ renderFromTemplate(renderOptions: TemplateRenderOptions): Promise<any>;
11
+ generateSignedUrl(signedUrlOptions: SignedUrlOptions): Promise<any>;
12
+ }
13
+ export default Orshot;
package/dist/index.js ADDED
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Orshot = void 0;
4
+ const constants_1 = require("./constants");
5
+ class Orshot {
6
+ apiKey;
7
+ constructor(apiKey) {
8
+ if (!apiKey) {
9
+ throw new Error("API Key is required.");
10
+ }
11
+ this.apiKey = apiKey;
12
+ }
13
+ getBaseUrl(version) {
14
+ const baseUrl = constants_1.ORSHOT_API_BASE_URL;
15
+ let apiVersion = constants_1.ORSHOT_API_VERSION;
16
+ if (version) {
17
+ apiVersion = version;
18
+ }
19
+ return `${baseUrl}/${apiVersion}`;
20
+ }
21
+ getHeaders() {
22
+ return {
23
+ 'Content-Type': 'application/json',
24
+ 'Authorization': `Bearer ${this.apiKey}`
25
+ };
26
+ }
27
+ async renderFromTemplate(renderOptions) {
28
+ let { templateId, modifications, responseType, responseFormat } = renderOptions;
29
+ if (!responseType) {
30
+ responseType = constants_1.DEFAULT_RESPONSE_TYPE;
31
+ }
32
+ if (!responseFormat) {
33
+ responseFormat = constants_1.DEFAULT_RESPONSE_FORMAT;
34
+ }
35
+ let endpoint = `${this.getBaseUrl()}/generate/images/${templateId}`;
36
+ const response = await fetch(endpoint, {
37
+ method: "POST",
38
+ headers: this.getHeaders(),
39
+ body: JSON.stringify({
40
+ response: {
41
+ type: responseType,
42
+ format: responseFormat
43
+ },
44
+ modifications: modifications,
45
+ source: constants_1.ORSHOT_SOURCE
46
+ }),
47
+ });
48
+ if (!response.ok) {
49
+ throw new Error("Failed to fetch image: " + response.status);
50
+ }
51
+ if (responseType === "base64" || responseType === "url") {
52
+ const jsonData = await response.json();
53
+ return jsonData;
54
+ }
55
+ else {
56
+ return response;
57
+ }
58
+ }
59
+ async generateSignedUrl(signedUrlOptions) {
60
+ let { templateId, modifications, renderType, responseFormat, expiresAt } = signedUrlOptions;
61
+ if (!renderType) {
62
+ renderType = constants_1.DEFAULT_RENDER_TYPE;
63
+ }
64
+ if (!responseFormat) {
65
+ responseFormat = constants_1.DEFAULT_RESPONSE_FORMAT;
66
+ }
67
+ let endpoint = `${this.getBaseUrl()}/signed-url/create`;
68
+ const response = await fetch(endpoint, {
69
+ method: "POST",
70
+ headers: this.getHeaders(),
71
+ body: JSON.stringify({
72
+ templateId: templateId,
73
+ renderType: renderType,
74
+ responseFormat: responseFormat,
75
+ modifications: modifications,
76
+ source: constants_1.ORSHOT_SOURCE,
77
+ expiresAt: expiresAt
78
+ }),
79
+ });
80
+ if (!response.ok) {
81
+ throw new Error("Failed to fetch image: " + response.status);
82
+ }
83
+ const jsonData = await response.json();
84
+ return jsonData;
85
+ }
86
+ }
87
+ exports.Orshot = Orshot;
88
+ exports.default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;QAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,yBAAyB,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC/D,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACvC,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF;AApGD,wBAoGC;AAED,kBAAe,MAAM,CAAC"}
@@ -0,0 +1,16 @@
1
+ export type ResponseType = "base64" | "binary" | "url";
2
+ export type ResponseFormat = "png" | "webp" | "pdf" | "jpg" | "jpeg";
3
+ export type RenderType = "images" | "pdfs";
4
+ export type TemplateRenderOptions = {
5
+ templateId: string;
6
+ modifications: any;
7
+ responseType?: ResponseType;
8
+ responseFormat?: ResponseFormat;
9
+ };
10
+ export type SignedUrlOptions = {
11
+ templateId: string;
12
+ modifications: any;
13
+ renderType?: RenderType;
14
+ responseFormat?: ResponseFormat;
15
+ expiresAt: number;
16
+ };
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
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",
3
+ "version": "0.1.2",
4
+ "description": "Orshot API SDK for Node.js",
5
+ "homepage": "https://orshot.com",
6
6
  "bugs": {
7
7
  "url": "https://github.com/rishimohan/orshot-nodejs-sdk/issues"
8
8
  },
package/src/constants.ts CHANGED
@@ -2,4 +2,5 @@ export const ORSHOT_SOURCE = "orshot-node-sdk";
2
2
  export const ORSHOT_API_BASE_URL = "https://api.orshot.com";
3
3
  export const ORSHOT_API_VERSION = "v1";
4
4
  export const DEFAULT_RESPONSE_TYPE = "base64";
5
- export const DEFAULT_RESPONSE_FORMAT = "png";
5
+ export const DEFAULT_RESPONSE_FORMAT = "png";
6
+ export const DEFAULT_RENDER_TYPE = "images";
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { TemplateRenderOptions } from "./types";
2
- import { DEFAULT_RESPONSE_TYPE, DEFAULT_RESPONSE_FORMAT, ORSHOT_API_BASE_URL, ORSHOT_API_VERSION, ORSHOT_SOURCE } from "./constants";
1
+ import { TemplateRenderOptions, SignedUrlOptions } from "./types";
2
+ import { DEFAULT_RESPONSE_TYPE, DEFAULT_RESPONSE_FORMAT, DEFAULT_RENDER_TYPE, ORSHOT_API_BASE_URL, ORSHOT_API_VERSION, ORSHOT_SOURCE } from "./constants";
3
3
 
4
4
  export class Orshot {
5
5
  private readonly apiKey: string;
@@ -67,6 +67,40 @@ export class Orshot {
67
67
  return response;
68
68
  }
69
69
  }
70
+
71
+ public async generateSignedUrl(signedUrlOptions: SignedUrlOptions) {
72
+ let { templateId, modifications, renderType, responseFormat, expiresAt } = signedUrlOptions;
73
+
74
+ if (!renderType) {
75
+ renderType = DEFAULT_RENDER_TYPE;
76
+ }
77
+
78
+ if (!responseFormat) {
79
+ responseFormat = DEFAULT_RESPONSE_FORMAT;
80
+ }
81
+
82
+ let endpoint = `${this.getBaseUrl()}/signed-url/create`;
83
+
84
+ const response = await fetch(endpoint, {
85
+ method: "POST",
86
+ headers: this.getHeaders(),
87
+ body: JSON.stringify({
88
+ templateId: templateId,
89
+ renderType: renderType,
90
+ responseFormat: responseFormat,
91
+ modifications: modifications,
92
+ source: ORSHOT_SOURCE,
93
+ expiresAt: expiresAt
94
+ }),
95
+ });
96
+
97
+ if (!response.ok) {
98
+ throw new Error("Failed to fetch image: " + response.status);
99
+ }
100
+
101
+ const jsonData = await response.json();
102
+ return jsonData;
103
+ }
70
104
  }
71
105
 
72
106
  export default Orshot;
package/src/types.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export type ResponseType = "base64" | "binary" | "url";
2
2
  export type ResponseFormat = "png" | "webp" | "pdf" | "jpg" | "jpeg" ;
3
+ export type RenderType = "images" | "pdfs";
3
4
 
4
5
  export type TemplateRenderOptions = {
5
6
  templateId: string;
@@ -7,3 +8,11 @@ export type TemplateRenderOptions = {
7
8
  responseType?: ResponseType;
8
9
  responseFormat?: ResponseFormat;
9
10
  };
11
+
12
+ export type SignedUrlOptions = {
13
+ templateId: string;
14
+ modifications: any;
15
+ renderType?: RenderType;
16
+ responseFormat?: ResponseFormat;
17
+ expiresAt: number;
18
+ };