@ministerjs/signing 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,158 @@
1
+ interface CreateDocFromPdfBaseOptions<AT extends Record<string, true>> {
2
+ name: string;
3
+ signers: {
4
+ name: string;
5
+ authType: AT;
6
+ email?: string;
7
+ cpf?: string;
8
+ sendAutomaticEmail?: boolean;
9
+ }[];
10
+ disableSignerEmails?: boolean;
11
+ reference?: string;
12
+ folderId?: string;
13
+ folderPath?: string;
14
+ }
15
+ interface CreateDocFromPdfWithBase64<AT extends Record<string, true>> extends CreateDocFromPdfBaseOptions<AT> {
16
+ fileBase64: string;
17
+ fileUrl?: never;
18
+ }
19
+ interface CreateDocFromPdfWithUrl<AT extends Record<string, true>> extends CreateDocFromPdfBaseOptions<AT> {
20
+ fileBase64?: never;
21
+ fileUrl: string;
22
+ }
23
+ type CreateDocFromPdfOptions<AT extends Record<string, true>> = CreateDocFromPdfWithBase64<AT> | CreateDocFromPdfWithUrl<AT>;
24
+ interface CreateDocFromDocxBaseOptions<AT extends Record<string, true>> {
25
+ name: string;
26
+ signers: {
27
+ name: string;
28
+ authType: AT;
29
+ email?: string;
30
+ cpf?: string;
31
+ sendAutomaticEmail?: boolean;
32
+ }[];
33
+ disableSignerEmails?: boolean;
34
+ reference?: string;
35
+ folderId?: string;
36
+ folderPath?: string;
37
+ }
38
+ interface CreateDocFromDocxWithBase64<AT extends Record<string, true>> extends CreateDocFromDocxBaseOptions<AT> {
39
+ fileBase64: string;
40
+ fileUrl?: never;
41
+ }
42
+ interface CreateDocFromDocxWithUrl<AT extends Record<string, true>> extends CreateDocFromDocxBaseOptions<AT> {
43
+ fileBase64?: never;
44
+ fileUrl: string;
45
+ }
46
+ type CreateDocFromDocxOptions<AT extends Record<string, true>> = CreateDocFromDocxWithBase64<AT> | CreateDocFromDocxWithUrl<AT>;
47
+ interface ListDocsOptions {
48
+ page?: number;
49
+ folderPath?: string;
50
+ deleted?: boolean;
51
+ status?: "pending" | "signed" | "refused";
52
+ }
53
+ interface Doc {
54
+ id: string;
55
+ status: "pending" | "signed" | "refused";
56
+ name: string;
57
+ originalFile: string;
58
+ signedFile: string | null;
59
+ signers?: {
60
+ name: string;
61
+ email: string;
62
+ signUrl: string;
63
+ }[];
64
+ createdAt: string;
65
+ }
66
+ interface ExtraDoc {
67
+ id: string;
68
+ name: string;
69
+ originalFile: string;
70
+ signedFile: string | null;
71
+ createdAt?: string;
72
+ }
73
+ interface AddExtraDocOptionsBase {
74
+ docId: string;
75
+ name?: string;
76
+ }
77
+ interface AddExtraDocWithBase64 extends AddExtraDocOptionsBase {
78
+ fileBase64: string;
79
+ fileUrl?: never;
80
+ }
81
+ interface AddExtraDocWithUrl extends AddExtraDocOptionsBase {
82
+ fileBase64?: never;
83
+ fileUrl: string;
84
+ }
85
+ type AddExtraDocOptions = AddExtraDocWithBase64 | AddExtraDocWithUrl;
86
+ interface GetRes<T> {
87
+ data: T;
88
+ }
89
+ interface ListRes<T> {
90
+ data: T[];
91
+ pageInfo?: {
92
+ totalResults: number;
93
+ next: string | null;
94
+ previous: string | null;
95
+ };
96
+ }
97
+ interface SigningService<AT extends Record<string, true>> extends EventTarget {
98
+ createDocFromPdf(data: CreateDocFromPdfWithBase64<AT>): Promise<GetRes<Doc>>;
99
+ createDocFromPdf(data: CreateDocFromPdfWithUrl<AT>): Promise<GetRes<Doc>>;
100
+ createDocFromPdf(data: CreateDocFromPdfOptions<AT>): Promise<GetRes<Doc>>;
101
+ createDocFromDocx(data: CreateDocFromDocxWithBase64<AT>): Promise<GetRes<Doc>>;
102
+ createDocFromDocx(data: CreateDocFromDocxWithUrl<AT>): Promise<GetRes<Doc>>;
103
+ createDocFromDocx(data: CreateDocFromDocxOptions<AT>): Promise<GetRes<Doc>>;
104
+ getDoc(id: string): Promise<GetRes<Doc>>;
105
+ listDocs(options?: ListDocsOptions): Promise<ListRes<Omit<Doc, "signers">>>;
106
+ listAllDocs(options?: Omit<ListDocsOptions, "page">): Promise<ListRes<Omit<Doc, "signers" | "listAllDocs">>>;
107
+ listAllDocsStream(options?: Omit<ListDocsOptions, "page">): AsyncGenerator<Omit<Doc, "signers" | "listAllDocs">>;
108
+ addExtraDoc(options: AddExtraDocWithBase64): Promise<GetRes<ExtraDoc>>;
109
+ addExtraDoc(options: AddExtraDocWithUrl): Promise<GetRes<ExtraDoc>>;
110
+ addExtraDoc(options: AddExtraDocOptions): Promise<GetRes<ExtraDoc>>;
111
+ }
112
+
113
+ type ExtraFields = {
114
+ selfiePhoto?: true;
115
+ documentPhoto?: true;
116
+ };
117
+ type ZapSignAuthType = ({
118
+ signatureOnScreen: true;
119
+ } & ExtraFields) | ({
120
+ tokenEmail: true;
121
+ } & ExtraFields) | ({
122
+ tokenSms: true;
123
+ } & ExtraFields) | ({
124
+ tokenWhatsapp: true;
125
+ } & ExtraFields) | ({
126
+ certificadoDigital: true;
127
+ } & ExtraFields) | ({
128
+ signatureOnScreen: true;
129
+ tokenEmail: true;
130
+ } & ExtraFields) | ({
131
+ signatureOnScreen: true;
132
+ tokenSms: true;
133
+ } & ExtraFields) | ({
134
+ signatureOnScreen: true;
135
+ tokenWhatsapp: true;
136
+ } & ExtraFields);
137
+ interface Options {
138
+ apiKey: string;
139
+ apiUrl?: string;
140
+ }
141
+ declare class ZapSign extends EventTarget implements SigningService<ZapSignAuthType> {
142
+ private apiKey;
143
+ private apiUrl;
144
+ private headers;
145
+ constructor({ apiKey, apiUrl }: Options);
146
+ createDocFromPdf(options: CreateDocFromPdfWithBase64<ZapSignAuthType>): Promise<GetRes<Doc>>;
147
+ createDocFromPdf(options: CreateDocFromPdfWithUrl<ZapSignAuthType>): Promise<GetRes<Doc>>;
148
+ createDocFromDocx(options: CreateDocFromDocxWithBase64<ZapSignAuthType>): Promise<GetRes<Doc>>;
149
+ createDocFromDocx(options: CreateDocFromDocxWithUrl<ZapSignAuthType>): Promise<GetRes<Doc>>;
150
+ getDoc(token: string): Promise<GetRes<Doc>>;
151
+ listDocs(options?: ListDocsOptions): Promise<ListRes<Omit<Doc, "signers">>>;
152
+ listAllDocs(options?: Omit<ListDocsOptions, "page">): Promise<ListRes<Omit<Doc, "signers" | "listAllDocs">>>;
153
+ listAllDocsStream(options?: Omit<ListDocsOptions, "page">): AsyncGenerator<Omit<Doc, "signers" | "listAllDocs">, void, void>;
154
+ addExtraDoc(options: AddExtraDocWithBase64): Promise<GetRes<ExtraDoc>>;
155
+ addExtraDoc(options: AddExtraDocWithUrl): Promise<GetRes<ExtraDoc>>;
156
+ }
157
+
158
+ export { type AddExtraDocOptions, type AddExtraDocOptionsBase, type AddExtraDocWithBase64, type AddExtraDocWithUrl, type CreateDocFromDocxOptions, type CreateDocFromDocxWithBase64, type CreateDocFromDocxWithUrl, type CreateDocFromPdfOptions, type CreateDocFromPdfWithBase64, type CreateDocFromPdfWithUrl, type Doc, type ExtraDoc, type GetRes, type ListDocsOptions, type ListRes, type SigningService, ZapSign, type ZapSignAuthType };
@@ -0,0 +1,158 @@
1
+ interface CreateDocFromPdfBaseOptions<AT extends Record<string, true>> {
2
+ name: string;
3
+ signers: {
4
+ name: string;
5
+ authType: AT;
6
+ email?: string;
7
+ cpf?: string;
8
+ sendAutomaticEmail?: boolean;
9
+ }[];
10
+ disableSignerEmails?: boolean;
11
+ reference?: string;
12
+ folderId?: string;
13
+ folderPath?: string;
14
+ }
15
+ interface CreateDocFromPdfWithBase64<AT extends Record<string, true>> extends CreateDocFromPdfBaseOptions<AT> {
16
+ fileBase64: string;
17
+ fileUrl?: never;
18
+ }
19
+ interface CreateDocFromPdfWithUrl<AT extends Record<string, true>> extends CreateDocFromPdfBaseOptions<AT> {
20
+ fileBase64?: never;
21
+ fileUrl: string;
22
+ }
23
+ type CreateDocFromPdfOptions<AT extends Record<string, true>> = CreateDocFromPdfWithBase64<AT> | CreateDocFromPdfWithUrl<AT>;
24
+ interface CreateDocFromDocxBaseOptions<AT extends Record<string, true>> {
25
+ name: string;
26
+ signers: {
27
+ name: string;
28
+ authType: AT;
29
+ email?: string;
30
+ cpf?: string;
31
+ sendAutomaticEmail?: boolean;
32
+ }[];
33
+ disableSignerEmails?: boolean;
34
+ reference?: string;
35
+ folderId?: string;
36
+ folderPath?: string;
37
+ }
38
+ interface CreateDocFromDocxWithBase64<AT extends Record<string, true>> extends CreateDocFromDocxBaseOptions<AT> {
39
+ fileBase64: string;
40
+ fileUrl?: never;
41
+ }
42
+ interface CreateDocFromDocxWithUrl<AT extends Record<string, true>> extends CreateDocFromDocxBaseOptions<AT> {
43
+ fileBase64?: never;
44
+ fileUrl: string;
45
+ }
46
+ type CreateDocFromDocxOptions<AT extends Record<string, true>> = CreateDocFromDocxWithBase64<AT> | CreateDocFromDocxWithUrl<AT>;
47
+ interface ListDocsOptions {
48
+ page?: number;
49
+ folderPath?: string;
50
+ deleted?: boolean;
51
+ status?: "pending" | "signed" | "refused";
52
+ }
53
+ interface Doc {
54
+ id: string;
55
+ status: "pending" | "signed" | "refused";
56
+ name: string;
57
+ originalFile: string;
58
+ signedFile: string | null;
59
+ signers?: {
60
+ name: string;
61
+ email: string;
62
+ signUrl: string;
63
+ }[];
64
+ createdAt: string;
65
+ }
66
+ interface ExtraDoc {
67
+ id: string;
68
+ name: string;
69
+ originalFile: string;
70
+ signedFile: string | null;
71
+ createdAt?: string;
72
+ }
73
+ interface AddExtraDocOptionsBase {
74
+ docId: string;
75
+ name?: string;
76
+ }
77
+ interface AddExtraDocWithBase64 extends AddExtraDocOptionsBase {
78
+ fileBase64: string;
79
+ fileUrl?: never;
80
+ }
81
+ interface AddExtraDocWithUrl extends AddExtraDocOptionsBase {
82
+ fileBase64?: never;
83
+ fileUrl: string;
84
+ }
85
+ type AddExtraDocOptions = AddExtraDocWithBase64 | AddExtraDocWithUrl;
86
+ interface GetRes<T> {
87
+ data: T;
88
+ }
89
+ interface ListRes<T> {
90
+ data: T[];
91
+ pageInfo?: {
92
+ totalResults: number;
93
+ next: string | null;
94
+ previous: string | null;
95
+ };
96
+ }
97
+ interface SigningService<AT extends Record<string, true>> extends EventTarget {
98
+ createDocFromPdf(data: CreateDocFromPdfWithBase64<AT>): Promise<GetRes<Doc>>;
99
+ createDocFromPdf(data: CreateDocFromPdfWithUrl<AT>): Promise<GetRes<Doc>>;
100
+ createDocFromPdf(data: CreateDocFromPdfOptions<AT>): Promise<GetRes<Doc>>;
101
+ createDocFromDocx(data: CreateDocFromDocxWithBase64<AT>): Promise<GetRes<Doc>>;
102
+ createDocFromDocx(data: CreateDocFromDocxWithUrl<AT>): Promise<GetRes<Doc>>;
103
+ createDocFromDocx(data: CreateDocFromDocxOptions<AT>): Promise<GetRes<Doc>>;
104
+ getDoc(id: string): Promise<GetRes<Doc>>;
105
+ listDocs(options?: ListDocsOptions): Promise<ListRes<Omit<Doc, "signers">>>;
106
+ listAllDocs(options?: Omit<ListDocsOptions, "page">): Promise<ListRes<Omit<Doc, "signers" | "listAllDocs">>>;
107
+ listAllDocsStream(options?: Omit<ListDocsOptions, "page">): AsyncGenerator<Omit<Doc, "signers" | "listAllDocs">>;
108
+ addExtraDoc(options: AddExtraDocWithBase64): Promise<GetRes<ExtraDoc>>;
109
+ addExtraDoc(options: AddExtraDocWithUrl): Promise<GetRes<ExtraDoc>>;
110
+ addExtraDoc(options: AddExtraDocOptions): Promise<GetRes<ExtraDoc>>;
111
+ }
112
+
113
+ type ExtraFields = {
114
+ selfiePhoto?: true;
115
+ documentPhoto?: true;
116
+ };
117
+ type ZapSignAuthType = ({
118
+ signatureOnScreen: true;
119
+ } & ExtraFields) | ({
120
+ tokenEmail: true;
121
+ } & ExtraFields) | ({
122
+ tokenSms: true;
123
+ } & ExtraFields) | ({
124
+ tokenWhatsapp: true;
125
+ } & ExtraFields) | ({
126
+ certificadoDigital: true;
127
+ } & ExtraFields) | ({
128
+ signatureOnScreen: true;
129
+ tokenEmail: true;
130
+ } & ExtraFields) | ({
131
+ signatureOnScreen: true;
132
+ tokenSms: true;
133
+ } & ExtraFields) | ({
134
+ signatureOnScreen: true;
135
+ tokenWhatsapp: true;
136
+ } & ExtraFields);
137
+ interface Options {
138
+ apiKey: string;
139
+ apiUrl?: string;
140
+ }
141
+ declare class ZapSign extends EventTarget implements SigningService<ZapSignAuthType> {
142
+ private apiKey;
143
+ private apiUrl;
144
+ private headers;
145
+ constructor({ apiKey, apiUrl }: Options);
146
+ createDocFromPdf(options: CreateDocFromPdfWithBase64<ZapSignAuthType>): Promise<GetRes<Doc>>;
147
+ createDocFromPdf(options: CreateDocFromPdfWithUrl<ZapSignAuthType>): Promise<GetRes<Doc>>;
148
+ createDocFromDocx(options: CreateDocFromDocxWithBase64<ZapSignAuthType>): Promise<GetRes<Doc>>;
149
+ createDocFromDocx(options: CreateDocFromDocxWithUrl<ZapSignAuthType>): Promise<GetRes<Doc>>;
150
+ getDoc(token: string): Promise<GetRes<Doc>>;
151
+ listDocs(options?: ListDocsOptions): Promise<ListRes<Omit<Doc, "signers">>>;
152
+ listAllDocs(options?: Omit<ListDocsOptions, "page">): Promise<ListRes<Omit<Doc, "signers" | "listAllDocs">>>;
153
+ listAllDocsStream(options?: Omit<ListDocsOptions, "page">): AsyncGenerator<Omit<Doc, "signers" | "listAllDocs">, void, void>;
154
+ addExtraDoc(options: AddExtraDocWithBase64): Promise<GetRes<ExtraDoc>>;
155
+ addExtraDoc(options: AddExtraDocWithUrl): Promise<GetRes<ExtraDoc>>;
156
+ }
157
+
158
+ export { type AddExtraDocOptions, type AddExtraDocOptionsBase, type AddExtraDocWithBase64, type AddExtraDocWithUrl, type CreateDocFromDocxOptions, type CreateDocFromDocxWithBase64, type CreateDocFromDocxWithUrl, type CreateDocFromPdfOptions, type CreateDocFromPdfWithBase64, type CreateDocFromPdfWithUrl, type Doc, type ExtraDoc, type GetRes, type ListDocsOptions, type ListRes, type SigningService, ZapSign, type ZapSignAuthType };
@@ -0,0 +1,343 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ ZapSign: () => ZapSign
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/ZapSign.ts
28
+ var ZapSign = class extends EventTarget {
29
+ apiKey;
30
+ apiUrl;
31
+ headers;
32
+ constructor({ apiKey, apiUrl }) {
33
+ super();
34
+ if (!apiKey) {
35
+ throw new Error("API key is required");
36
+ }
37
+ this.apiKey = apiKey;
38
+ this.apiUrl = apiUrl || "https://sandbox.api.zapsign.com.br/api/v1";
39
+ this.headers = {
40
+ Authorization: `Bearer ${this.apiKey}`,
41
+ "Content-Type": "application/json"
42
+ };
43
+ }
44
+ async createDocFromPdf({
45
+ name,
46
+ fileBase64,
47
+ fileUrl,
48
+ signers,
49
+ disableSignerEmails,
50
+ reference,
51
+ folderId,
52
+ folderPath
53
+ }) {
54
+ const url = `${this.apiUrl}/docs/`;
55
+ if (!fileBase64 && !fileUrl) {
56
+ throw new Error("Either fileBase64 or fileUrl must be provided");
57
+ }
58
+ if (fileBase64 && fileUrl) {
59
+ throw new Error("Only one of fileBase64 or fileUrl should be provided");
60
+ }
61
+ const mappedSigners = signers.map((signer) => {
62
+ const { name: name2, email, cpf, authType } = signer;
63
+ const authModeMap = [
64
+ [
65
+ "assinaturaTela-tokenEmail",
66
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenEmail")
67
+ ],
68
+ [
69
+ "assinaturaTela-tokenSms",
70
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenSms")
71
+ ],
72
+ [
73
+ "assinaturaTela-tokenWhatsapp",
74
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenWhatsapp")
75
+ ],
76
+ ["assinaturaTela", Reflect.get(authType, "signatureOnScreen")],
77
+ ["tokenEmail", Reflect.get(authType, "tokenEmail")],
78
+ ["tokenSms", Reflect.get(authType, "tokenSms")],
79
+ ["tokenWhatsapp", Reflect.get(authType, "tokenWhatsapp")],
80
+ ["certificadoDigital", Reflect.get(authType, "certificadoDigital")]
81
+ ];
82
+ const auth_mode = authModeMap.find(([, value]) => value)?.[0];
83
+ return {
84
+ name: name2,
85
+ email,
86
+ cpf,
87
+ auth_mode,
88
+ require_selfie_photo: authType.selfiePhoto,
89
+ require_document_photo: authType.documentPhoto,
90
+ send_automatic_email: signer.sendAutomaticEmail || false,
91
+ lock_name: true,
92
+ lock_email: true
93
+ };
94
+ });
95
+ const body = JSON.stringify({
96
+ name,
97
+ url_pdf: fileUrl,
98
+ base64_pdf: fileBase64,
99
+ signers: mappedSigners,
100
+ disable_signer_emails: disableSignerEmails || false,
101
+ external_id: reference,
102
+ folder_token: folderId,
103
+ folder_path: folderPath
104
+ });
105
+ const res = await fetch(url, {
106
+ method: "POST",
107
+ headers: this.headers,
108
+ body
109
+ });
110
+ if (!res.ok) throw new Error(await res.text());
111
+ const parsedData = await res.json();
112
+ return {
113
+ data: {
114
+ id: parsedData.token,
115
+ status: parsedData.status,
116
+ name: parsedData.name,
117
+ originalFile: parsedData.original_file,
118
+ signedFile: parsedData.signed_file,
119
+ signers: parsedData.signers.map((signer) => ({
120
+ name: signer.name,
121
+ email: signer.email,
122
+ signUrl: signer.sign_url
123
+ })),
124
+ createdAt: parsedData.created_at
125
+ }
126
+ };
127
+ }
128
+ async createDocFromDocx({
129
+ name,
130
+ fileBase64,
131
+ fileUrl,
132
+ signers,
133
+ disableSignerEmails,
134
+ reference,
135
+ folderId,
136
+ folderPath
137
+ }) {
138
+ const url = `${this.apiUrl}/docs/`;
139
+ if (!fileBase64 && !fileUrl) {
140
+ throw new Error("Either fileBase64 or fileUrl must be provided");
141
+ }
142
+ if (fileBase64 && fileUrl) {
143
+ throw new Error("Only one of fileBase64 or fileUrl should be provided");
144
+ }
145
+ const mappedSigners = signers.map((signer) => {
146
+ const { name: name2, email, cpf, authType } = signer;
147
+ const authModeMap = [
148
+ [
149
+ "assinaturaTela-tokenEmail",
150
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenEmail")
151
+ ],
152
+ [
153
+ "assinaturaTela-tokenSms",
154
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenSms")
155
+ ],
156
+ [
157
+ "assinaturaTela-tokenWhatsapp",
158
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenWhatsapp")
159
+ ],
160
+ ["assinaturaTela", Reflect.get(authType, "signatureOnScreen")],
161
+ ["tokenEmail", Reflect.get(authType, "tokenEmail")],
162
+ ["tokenSms", Reflect.get(authType, "tokenSms")],
163
+ ["tokenWhatsapp", Reflect.get(authType, "tokenWhatsapp")],
164
+ ["certificadoDigital", Reflect.get(authType, "certificadoDigital")]
165
+ ];
166
+ const auth_mode = authModeMap.find(([, value]) => value)?.[0];
167
+ return {
168
+ name: name2,
169
+ email,
170
+ cpf,
171
+ auth_mode,
172
+ require_selfie_photo: authType.selfiePhoto,
173
+ require_document_photo: authType.documentPhoto,
174
+ send_automatic_email: signer.sendAutomaticEmail || false,
175
+ lock_name: true,
176
+ lock_email: true
177
+ };
178
+ });
179
+ const body = JSON.stringify({
180
+ name,
181
+ url_docx: fileUrl,
182
+ base64_docx: fileBase64,
183
+ signers: mappedSigners,
184
+ disable_signer_emails: disableSignerEmails || false,
185
+ external_id: reference,
186
+ folder_token: folderId,
187
+ folder_path: folderPath
188
+ });
189
+ const res = await fetch(url, {
190
+ method: "POST",
191
+ headers: this.headers,
192
+ body
193
+ });
194
+ if (!res.ok) throw new Error(await res.text());
195
+ const parsedData = await res.json();
196
+ return {
197
+ data: {
198
+ id: parsedData.token,
199
+ status: parsedData.status,
200
+ name: parsedData.name,
201
+ originalFile: parsedData.original_file,
202
+ signedFile: parsedData.signed_file,
203
+ signers: parsedData.signers.map((signer) => ({
204
+ name: signer.name,
205
+ email: signer.email,
206
+ signUrl: signer.sign_url
207
+ })),
208
+ createdAt: parsedData.created_at
209
+ }
210
+ };
211
+ }
212
+ async getDoc(token) {
213
+ const response = await fetch(
214
+ `https://api.zapsign.com.br/api/v1/docs/${token}/`,
215
+ {
216
+ method: "GET",
217
+ headers: this.headers
218
+ }
219
+ );
220
+ if (!response.ok) throw new Error(await response.text());
221
+ const data = await response.json();
222
+ return {
223
+ data: {
224
+ id: data.token,
225
+ status: data.status,
226
+ name: data.name,
227
+ originalFile: data.original_file,
228
+ signedFile: data.signed_file,
229
+ signers: data.signers?.map((signer) => ({
230
+ name: signer.name,
231
+ email: signer.email,
232
+ signUrl: `https://app.zapsign.com.br/verificar/${signer.token}`
233
+ })),
234
+ createdAt: data.created_at
235
+ }
236
+ };
237
+ }
238
+ async listDocs(options) {
239
+ let url = `${this.apiUrl}/docs/`;
240
+ url += `?page=${options?.page || 1}`;
241
+ if (options?.folderPath) {
242
+ url += `&folder_path=${encodeURIComponent(options.folderPath)}`;
243
+ }
244
+ if (options?.deleted) {
245
+ url += `&deleted=${options.deleted}`;
246
+ }
247
+ if (options?.status) {
248
+ url += `&status=${options.status}`;
249
+ }
250
+ const response = await fetch(url, {
251
+ method: "GET",
252
+ headers: this.headers
253
+ });
254
+ if (!response.ok) throw new Error(await response.text());
255
+ const parsedData = await response.json();
256
+ const data = parsedData.results.map((doc) => ({
257
+ id: doc.token,
258
+ status: doc.status,
259
+ name: doc.name,
260
+ originalFile: doc.original_file,
261
+ signedFile: doc.signed_file,
262
+ createdAt: doc.created_at
263
+ }));
264
+ return {
265
+ data,
266
+ pageInfo: {
267
+ totalResults: parsedData.count,
268
+ next: parsedData.next,
269
+ previous: parsedData.previous
270
+ }
271
+ };
272
+ }
273
+ async listAllDocs(options) {
274
+ const allDocs = [];
275
+ for await (const doc of this.listAllDocsStream(options)) {
276
+ allDocs.push(doc);
277
+ }
278
+ return {
279
+ data: allDocs
280
+ };
281
+ }
282
+ async *listAllDocsStream(options) {
283
+ let page = 1;
284
+ let hasMore = true;
285
+ let count = 0;
286
+ while (hasMore) {
287
+ const { data, pageInfo } = await this.listDocs({ ...options, page });
288
+ count += data.length;
289
+ this.dispatchEvent(
290
+ new CustomEvent("list-all-progress", {
291
+ detail: {
292
+ data,
293
+ count
294
+ }
295
+ })
296
+ );
297
+ for (const doc of data) {
298
+ yield doc;
299
+ }
300
+ page++;
301
+ hasMore = Boolean(pageInfo?.next);
302
+ }
303
+ }
304
+ async addExtraDoc({
305
+ docId,
306
+ fileBase64,
307
+ fileUrl,
308
+ name
309
+ }) {
310
+ if (!docId) throw new Error("docId \xE9 obrigat\xF3rio");
311
+ if (!fileBase64 && !fileUrl) {
312
+ throw new Error("Informe fileBase64 ou fileUrl");
313
+ }
314
+ if (fileBase64 && fileUrl) {
315
+ throw new Error("Apenas um de fileBase64 ou fileUrl deve ser enviado");
316
+ }
317
+ const url = `${this.apiUrl}/docs/${docId}/upload-extra-doc/`;
318
+ const body = JSON.stringify({
319
+ base64_pdf: fileBase64,
320
+ url_pdf: fileUrl,
321
+ name
322
+ });
323
+ const res = await fetch(url, {
324
+ method: "POST",
325
+ headers: this.headers,
326
+ body
327
+ });
328
+ if (!res.ok) throw new Error(await res.text());
329
+ const data = await res.json();
330
+ return {
331
+ data: {
332
+ id: data.token,
333
+ name: data.name,
334
+ originalFile: data.original_file,
335
+ signedFile: data.signed_file
336
+ }
337
+ };
338
+ }
339
+ };
340
+ // Annotate the CommonJS export names for ESM import in node:
341
+ 0 && (module.exports = {
342
+ ZapSign
343
+ });
@@ -0,0 +1,316 @@
1
+ // src/ZapSign.ts
2
+ var ZapSign = class extends EventTarget {
3
+ apiKey;
4
+ apiUrl;
5
+ headers;
6
+ constructor({ apiKey, apiUrl }) {
7
+ super();
8
+ if (!apiKey) {
9
+ throw new Error("API key is required");
10
+ }
11
+ this.apiKey = apiKey;
12
+ this.apiUrl = apiUrl || "https://sandbox.api.zapsign.com.br/api/v1";
13
+ this.headers = {
14
+ Authorization: `Bearer ${this.apiKey}`,
15
+ "Content-Type": "application/json"
16
+ };
17
+ }
18
+ async createDocFromPdf({
19
+ name,
20
+ fileBase64,
21
+ fileUrl,
22
+ signers,
23
+ disableSignerEmails,
24
+ reference,
25
+ folderId,
26
+ folderPath
27
+ }) {
28
+ const url = `${this.apiUrl}/docs/`;
29
+ if (!fileBase64 && !fileUrl) {
30
+ throw new Error("Either fileBase64 or fileUrl must be provided");
31
+ }
32
+ if (fileBase64 && fileUrl) {
33
+ throw new Error("Only one of fileBase64 or fileUrl should be provided");
34
+ }
35
+ const mappedSigners = signers.map((signer) => {
36
+ const { name: name2, email, cpf, authType } = signer;
37
+ const authModeMap = [
38
+ [
39
+ "assinaturaTela-tokenEmail",
40
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenEmail")
41
+ ],
42
+ [
43
+ "assinaturaTela-tokenSms",
44
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenSms")
45
+ ],
46
+ [
47
+ "assinaturaTela-tokenWhatsapp",
48
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenWhatsapp")
49
+ ],
50
+ ["assinaturaTela", Reflect.get(authType, "signatureOnScreen")],
51
+ ["tokenEmail", Reflect.get(authType, "tokenEmail")],
52
+ ["tokenSms", Reflect.get(authType, "tokenSms")],
53
+ ["tokenWhatsapp", Reflect.get(authType, "tokenWhatsapp")],
54
+ ["certificadoDigital", Reflect.get(authType, "certificadoDigital")]
55
+ ];
56
+ const auth_mode = authModeMap.find(([, value]) => value)?.[0];
57
+ return {
58
+ name: name2,
59
+ email,
60
+ cpf,
61
+ auth_mode,
62
+ require_selfie_photo: authType.selfiePhoto,
63
+ require_document_photo: authType.documentPhoto,
64
+ send_automatic_email: signer.sendAutomaticEmail || false,
65
+ lock_name: true,
66
+ lock_email: true
67
+ };
68
+ });
69
+ const body = JSON.stringify({
70
+ name,
71
+ url_pdf: fileUrl,
72
+ base64_pdf: fileBase64,
73
+ signers: mappedSigners,
74
+ disable_signer_emails: disableSignerEmails || false,
75
+ external_id: reference,
76
+ folder_token: folderId,
77
+ folder_path: folderPath
78
+ });
79
+ const res = await fetch(url, {
80
+ method: "POST",
81
+ headers: this.headers,
82
+ body
83
+ });
84
+ if (!res.ok) throw new Error(await res.text());
85
+ const parsedData = await res.json();
86
+ return {
87
+ data: {
88
+ id: parsedData.token,
89
+ status: parsedData.status,
90
+ name: parsedData.name,
91
+ originalFile: parsedData.original_file,
92
+ signedFile: parsedData.signed_file,
93
+ signers: parsedData.signers.map((signer) => ({
94
+ name: signer.name,
95
+ email: signer.email,
96
+ signUrl: signer.sign_url
97
+ })),
98
+ createdAt: parsedData.created_at
99
+ }
100
+ };
101
+ }
102
+ async createDocFromDocx({
103
+ name,
104
+ fileBase64,
105
+ fileUrl,
106
+ signers,
107
+ disableSignerEmails,
108
+ reference,
109
+ folderId,
110
+ folderPath
111
+ }) {
112
+ const url = `${this.apiUrl}/docs/`;
113
+ if (!fileBase64 && !fileUrl) {
114
+ throw new Error("Either fileBase64 or fileUrl must be provided");
115
+ }
116
+ if (fileBase64 && fileUrl) {
117
+ throw new Error("Only one of fileBase64 or fileUrl should be provided");
118
+ }
119
+ const mappedSigners = signers.map((signer) => {
120
+ const { name: name2, email, cpf, authType } = signer;
121
+ const authModeMap = [
122
+ [
123
+ "assinaturaTela-tokenEmail",
124
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenEmail")
125
+ ],
126
+ [
127
+ "assinaturaTela-tokenSms",
128
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenSms")
129
+ ],
130
+ [
131
+ "assinaturaTela-tokenWhatsapp",
132
+ Reflect.get(authType, "signatureOnScreen") && Reflect.get(authType, "tokenWhatsapp")
133
+ ],
134
+ ["assinaturaTela", Reflect.get(authType, "signatureOnScreen")],
135
+ ["tokenEmail", Reflect.get(authType, "tokenEmail")],
136
+ ["tokenSms", Reflect.get(authType, "tokenSms")],
137
+ ["tokenWhatsapp", Reflect.get(authType, "tokenWhatsapp")],
138
+ ["certificadoDigital", Reflect.get(authType, "certificadoDigital")]
139
+ ];
140
+ const auth_mode = authModeMap.find(([, value]) => value)?.[0];
141
+ return {
142
+ name: name2,
143
+ email,
144
+ cpf,
145
+ auth_mode,
146
+ require_selfie_photo: authType.selfiePhoto,
147
+ require_document_photo: authType.documentPhoto,
148
+ send_automatic_email: signer.sendAutomaticEmail || false,
149
+ lock_name: true,
150
+ lock_email: true
151
+ };
152
+ });
153
+ const body = JSON.stringify({
154
+ name,
155
+ url_docx: fileUrl,
156
+ base64_docx: fileBase64,
157
+ signers: mappedSigners,
158
+ disable_signer_emails: disableSignerEmails || false,
159
+ external_id: reference,
160
+ folder_token: folderId,
161
+ folder_path: folderPath
162
+ });
163
+ const res = await fetch(url, {
164
+ method: "POST",
165
+ headers: this.headers,
166
+ body
167
+ });
168
+ if (!res.ok) throw new Error(await res.text());
169
+ const parsedData = await res.json();
170
+ return {
171
+ data: {
172
+ id: parsedData.token,
173
+ status: parsedData.status,
174
+ name: parsedData.name,
175
+ originalFile: parsedData.original_file,
176
+ signedFile: parsedData.signed_file,
177
+ signers: parsedData.signers.map((signer) => ({
178
+ name: signer.name,
179
+ email: signer.email,
180
+ signUrl: signer.sign_url
181
+ })),
182
+ createdAt: parsedData.created_at
183
+ }
184
+ };
185
+ }
186
+ async getDoc(token) {
187
+ const response = await fetch(
188
+ `https://api.zapsign.com.br/api/v1/docs/${token}/`,
189
+ {
190
+ method: "GET",
191
+ headers: this.headers
192
+ }
193
+ );
194
+ if (!response.ok) throw new Error(await response.text());
195
+ const data = await response.json();
196
+ return {
197
+ data: {
198
+ id: data.token,
199
+ status: data.status,
200
+ name: data.name,
201
+ originalFile: data.original_file,
202
+ signedFile: data.signed_file,
203
+ signers: data.signers?.map((signer) => ({
204
+ name: signer.name,
205
+ email: signer.email,
206
+ signUrl: `https://app.zapsign.com.br/verificar/${signer.token}`
207
+ })),
208
+ createdAt: data.created_at
209
+ }
210
+ };
211
+ }
212
+ async listDocs(options) {
213
+ let url = `${this.apiUrl}/docs/`;
214
+ url += `?page=${options?.page || 1}`;
215
+ if (options?.folderPath) {
216
+ url += `&folder_path=${encodeURIComponent(options.folderPath)}`;
217
+ }
218
+ if (options?.deleted) {
219
+ url += `&deleted=${options.deleted}`;
220
+ }
221
+ if (options?.status) {
222
+ url += `&status=${options.status}`;
223
+ }
224
+ const response = await fetch(url, {
225
+ method: "GET",
226
+ headers: this.headers
227
+ });
228
+ if (!response.ok) throw new Error(await response.text());
229
+ const parsedData = await response.json();
230
+ const data = parsedData.results.map((doc) => ({
231
+ id: doc.token,
232
+ status: doc.status,
233
+ name: doc.name,
234
+ originalFile: doc.original_file,
235
+ signedFile: doc.signed_file,
236
+ createdAt: doc.created_at
237
+ }));
238
+ return {
239
+ data,
240
+ pageInfo: {
241
+ totalResults: parsedData.count,
242
+ next: parsedData.next,
243
+ previous: parsedData.previous
244
+ }
245
+ };
246
+ }
247
+ async listAllDocs(options) {
248
+ const allDocs = [];
249
+ for await (const doc of this.listAllDocsStream(options)) {
250
+ allDocs.push(doc);
251
+ }
252
+ return {
253
+ data: allDocs
254
+ };
255
+ }
256
+ async *listAllDocsStream(options) {
257
+ let page = 1;
258
+ let hasMore = true;
259
+ let count = 0;
260
+ while (hasMore) {
261
+ const { data, pageInfo } = await this.listDocs({ ...options, page });
262
+ count += data.length;
263
+ this.dispatchEvent(
264
+ new CustomEvent("list-all-progress", {
265
+ detail: {
266
+ data,
267
+ count
268
+ }
269
+ })
270
+ );
271
+ for (const doc of data) {
272
+ yield doc;
273
+ }
274
+ page++;
275
+ hasMore = Boolean(pageInfo?.next);
276
+ }
277
+ }
278
+ async addExtraDoc({
279
+ docId,
280
+ fileBase64,
281
+ fileUrl,
282
+ name
283
+ }) {
284
+ if (!docId) throw new Error("docId \xE9 obrigat\xF3rio");
285
+ if (!fileBase64 && !fileUrl) {
286
+ throw new Error("Informe fileBase64 ou fileUrl");
287
+ }
288
+ if (fileBase64 && fileUrl) {
289
+ throw new Error("Apenas um de fileBase64 ou fileUrl deve ser enviado");
290
+ }
291
+ const url = `${this.apiUrl}/docs/${docId}/upload-extra-doc/`;
292
+ const body = JSON.stringify({
293
+ base64_pdf: fileBase64,
294
+ url_pdf: fileUrl,
295
+ name
296
+ });
297
+ const res = await fetch(url, {
298
+ method: "POST",
299
+ headers: this.headers,
300
+ body
301
+ });
302
+ if (!res.ok) throw new Error(await res.text());
303
+ const data = await res.json();
304
+ return {
305
+ data: {
306
+ id: data.token,
307
+ name: data.name,
308
+ originalFile: data.original_file,
309
+ signedFile: data.signed_file
310
+ }
311
+ };
312
+ }
313
+ };
314
+ export {
315
+ ZapSign
316
+ };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@ministerjs/signing",
3
+ "version": "0.1.0",
4
+ "license": "UNLICENSED",
5
+ "private": false,
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "files": [
10
+ "dist/*"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/signing.d.ts",
15
+ "import": "./dist/signing.mjs",
16
+ "require": "./dist/signing.js"
17
+ }
18
+ },
19
+ "devDependencies": {
20
+ "@eslint/js": "^9.32.0",
21
+ "@types/node": "^22.13.10",
22
+ "eslint": "^9.32.0",
23
+ "eslint-config-prettier": "^10.1.8",
24
+ "globals": "^16.3.0",
25
+ "prettier": "^3.6.2",
26
+ "tsup": "^8.5.0",
27
+ "tsx": "^4.20.3",
28
+ "typescript": "~5.8.3",
29
+ "typescript-eslint": "^8.38.0",
30
+ "vitest": "^3.2.4"
31
+ },
32
+ "scripts": {
33
+ "dev": "tsup --watch",
34
+ "lint": "eslint ./src --fix --config ../../eslint.config.mjs",
35
+ "test": "vitest run --root ../../ --project @ministerjs/signing",
36
+ "build": "pnpm lint && pnpm test && tsup"
37
+ }
38
+ }