mira-form-sdk 1.0.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.
- package/README.md +58 -0
- package/dist/index.cjs +92 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +91 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.mjs +65 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Mira Form SDK
|
|
2
|
+
|
|
3
|
+
SDK for Mira Form API на TypeScript/JavaScript.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install mira-form-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Start Using
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { MiraFormSDK, MiraFormSDKOptions } from 'mira-form-sdk';
|
|
15
|
+
|
|
16
|
+
const sdk = new MiraFormSDK('your-api-key', {
|
|
17
|
+
baseUrl: 'https://api.miraform.com',
|
|
18
|
+
timeout: 5000,
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Example Usage
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { MiraFormSDK } from 'mira-form-sdk';
|
|
26
|
+
|
|
27
|
+
const sdk = new MiraFormSDK('pk_key', { baseUrl: 'https://api.miraform.com' });
|
|
28
|
+
|
|
29
|
+
// Data preparation
|
|
30
|
+
const { formData, errors } = sdk.prepareFormData({
|
|
31
|
+
resourceId: 'uuid-ресурса',
|
|
32
|
+
formId: 'uuid-формы',
|
|
33
|
+
content: { field1: 'value1', field2: 123 },
|
|
34
|
+
files: [file1, file2], // optional
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (errors) {
|
|
38
|
+
console.error('Validation error:', errors);
|
|
39
|
+
} else {
|
|
40
|
+
// Form submission
|
|
41
|
+
sdk.sendForm(formData).then(response => {
|
|
42
|
+
if (response.success) {
|
|
43
|
+
console.log('Success:', response.data);
|
|
44
|
+
} else {
|
|
45
|
+
console.error('Error:', response.errors);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Build
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm run build
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
MiraFormSDK: () => MiraFormSDK
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/sdk.ts
|
|
28
|
+
var MiraFormSDK = class {
|
|
29
|
+
constructor(apiKey, options) {
|
|
30
|
+
this.apiKey = apiKey;
|
|
31
|
+
this.options = options || {};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Prepares form data for submission by validating the input parameters and appending them to a FormData object.
|
|
35
|
+
*
|
|
36
|
+
* @param {PrepareFormDataParams} params - The input parameters for preparing the form data.
|
|
37
|
+
* - resourceId: The ID of the resource (required).
|
|
38
|
+
* - formId: The ID of the form (required).
|
|
39
|
+
* - content: The content object to include (optional, must be a valid object).
|
|
40
|
+
* - files: An array of files to include in the form data (optional).
|
|
41
|
+
*
|
|
42
|
+
* @return {PreparedFormData} An object containing the prepared FormData object or error messages if validation fails.
|
|
43
|
+
*/
|
|
44
|
+
prepareFormData(params) {
|
|
45
|
+
const errors = [];
|
|
46
|
+
if (!params.resourceId)
|
|
47
|
+
errors.push("resourceId is required");
|
|
48
|
+
if (!params.formId)
|
|
49
|
+
errors.push("formId is required");
|
|
50
|
+
if (params.content && typeof params.content !== "object")
|
|
51
|
+
errors.push("content must be a valid object");
|
|
52
|
+
if (errors.length)
|
|
53
|
+
return { formData: null, errors };
|
|
54
|
+
const formData = new FormData();
|
|
55
|
+
formData.append("resourceId", params.resourceId);
|
|
56
|
+
formData.append("formId", params.formId);
|
|
57
|
+
formData.append("content", JSON.stringify(params.content));
|
|
58
|
+
if (params.files) {
|
|
59
|
+
params.files.forEach((file, idx) => {
|
|
60
|
+
formData.append(`file_${idx}`, file);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return { formData };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Sends a form to the specified API endpoint.
|
|
67
|
+
*
|
|
68
|
+
* @param {FormData} formData - The form data to be sent to the server.
|
|
69
|
+
* @return {Promise<SendFormResponse>} A promise that resolves to an object indicating the success of the operation, the server response data, and any errors encountered.
|
|
70
|
+
*/
|
|
71
|
+
async sendForm(formData) {
|
|
72
|
+
const url = `${this.options.baseUrl}/api/v1/send`;
|
|
73
|
+
try {
|
|
74
|
+
const res = await fetch(url, {
|
|
75
|
+
method: "POST",
|
|
76
|
+
headers: {
|
|
77
|
+
"Authentication": `Bearer ${this.apiKey}`
|
|
78
|
+
},
|
|
79
|
+
body: formData
|
|
80
|
+
});
|
|
81
|
+
const data = await res.json();
|
|
82
|
+
return { success: res.ok, data, errors: !res.ok ? [data?.message || "Unknown error"] : void 0 };
|
|
83
|
+
} catch (e) {
|
|
84
|
+
return { success: false, errors: [e.message] };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
MiraFormSDK
|
|
91
|
+
});
|
|
92
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/sdk.ts"],"sourcesContent":["export { MiraFormSDK } from './sdk';\nexport type { MiraFormSDKOptions } from './types';\n\n","import { MiraFormSDKOptions } from './types';\nimport {\n PrepareFormDataParams,\n PreparedFormData,\n SendFormResponse,\n} from './types';\n\n\n/**\n * A class to interact with the MiraForm API for creating and sending form data.\n */\nexport class MiraFormSDK {\n private readonly apiKey: string;\n private options: MiraFormSDKOptions;\n\n constructor(apiKey: string, options?: MiraFormSDKOptions) {\n this.apiKey = apiKey;\n this.options = options || {};\n }\n\n\n /**\n * Prepares form data for submission by validating the input parameters and appending them to a FormData object.\n *\n * @param {PrepareFormDataParams} params - The input parameters for preparing the form data.\n * - resourceId: The ID of the resource (required).\n * - formId: The ID of the form (required).\n * - content: The content object to include (optional, must be a valid object).\n * - files: An array of files to include in the form data (optional).\n *\n * @return {PreparedFormData} An object containing the prepared FormData object or error messages if validation fails.\n */\n public prepareFormData(params: PrepareFormDataParams): PreparedFormData {\n const errors: string[] = [];\n if (!params.resourceId) errors.push('resourceId is required');\n if (!params.formId) errors.push('formId is required');\n if (params.content && typeof params.content !== 'object') errors.push('content must be a valid object');\n if (errors.length) return { formData: null as any, errors };\n\n const formData = new FormData();\n formData.append('resourceId', params.resourceId);\n formData.append('formId', params.formId);\n formData.append('content', JSON.stringify(params.content));\n if (params.files) {\n params.files.forEach((file, idx) => {\n formData.append(`file_${idx}`, file as any);\n });\n }\n return { formData };\n }\n\n /**\n * Sends a form to the specified API endpoint.\n *\n * @param {FormData} formData - The form data to be sent to the server.\n * @return {Promise<SendFormResponse>} A promise that resolves to an object indicating the success of the operation, the server response data, and any errors encountered.\n */\n public async sendForm(formData: FormData): Promise<SendFormResponse> {\n const url = `${this.options.baseUrl}/api/v1/send`;\n try {\n const res = await fetch(url, {\n method: 'POST',\n headers: {\n 'Authentication': `Bearer ${this.apiKey}`,\n },\n body: formData,\n });\n const data = await res.json();\n return { success: res.ok, data, errors: !res.ok ? [data?.message || 'Unknown error'] : undefined };\n } catch (e: any) {\n return { success: false, errors: [e.message] };\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWO,IAAM,cAAN,MAAkB;AAAA,EAIvB,YAAY,QAAgB,SAA8B;AACxD,SAAK,SAAS;AACd,SAAK,UAAU,WAAW,CAAC;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcO,gBAAgB,QAAiD;AACtE,UAAM,SAAmB,CAAC;AAC1B,QAAI,CAAC,OAAO;AAAY,aAAO,KAAK,wBAAwB;AAC5D,QAAI,CAAC,OAAO;AAAQ,aAAO,KAAK,oBAAoB;AACpD,QAAI,OAAO,WAAW,OAAO,OAAO,YAAY;AAAU,aAAO,KAAK,gCAAgC;AACtG,QAAI,OAAO;AAAQ,aAAO,EAAE,UAAU,MAAa,OAAO;AAE1D,UAAM,WAAW,IAAI,SAAS;AAC9B,aAAS,OAAO,cAAc,OAAO,UAAU;AAC/C,aAAS,OAAO,UAAU,OAAO,MAAM;AACvC,aAAS,OAAO,WAAW,KAAK,UAAU,OAAO,OAAO,CAAC;AACzD,QAAI,OAAO,OAAO;AAChB,aAAO,MAAM,QAAQ,CAAC,MAAM,QAAQ;AAClC,iBAAS,OAAO,QAAQ,GAAG,IAAI,IAAW;AAAA,MAC5C,CAAC;AAAA,IACH;AACA,WAAO,EAAE,SAAS;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAa,SAAS,UAA+C;AACnE,UAAM,MAAM,GAAG,KAAK,QAAQ,OAAO;AACnC,QAAI;AACF,YAAM,MAAM,MAAM,MAAM,KAAK;AAAA,QAC3B,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,kBAAkB,UAAU,KAAK,MAAM;AAAA,QACzC;AAAA,QACA,MAAM;AAAA,MACR,CAAC;AACD,YAAM,OAAO,MAAM,IAAI,KAAK;AAC5B,aAAO,EAAE,SAAS,IAAI,IAAI,MAAM,QAAQ,CAAC,IAAI,KAAK,CAAC,MAAM,WAAW,eAAe,IAAI,OAAU;AAAA,IACnG,SAAS,GAAQ;AACf,aAAO,EAAE,SAAS,OAAO,QAAQ,CAAC,EAAE,OAAO,EAAE;AAAA,IAC/C;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for the MiraForm SDK.
|
|
3
|
+
* This interface defines the optional parameters that can be used
|
|
4
|
+
* to customize the behavior of the MiraForm SDK.
|
|
5
|
+
*
|
|
6
|
+
* @property baseUrl - The base URL to which the SDK will send requests. If not provided, a default URL may be used.
|
|
7
|
+
* @property timeout - Specifies the timeout duration (in milliseconds) for requests. If not set, a default timeout may apply.
|
|
8
|
+
*/
|
|
9
|
+
interface MiraFormSDKOptions {
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Represents the parameters required to prepare form data.
|
|
15
|
+
*
|
|
16
|
+
* This interface is used to encapsulate the necessary properties
|
|
17
|
+
* needed to prepare and manage form data, including associated files
|
|
18
|
+
* and form-specific identifiers.
|
|
19
|
+
*
|
|
20
|
+
* Properties:
|
|
21
|
+
* - `resourceId` (string): The unique identifier (UUID) for the resource being processed.
|
|
22
|
+
* - `formId` (string): The unique identifier (UUID) for the form.
|
|
23
|
+
* - `content` (Record<string, any>): A key-value mapping of the form's content data.
|
|
24
|
+
* - `files` (File[] | undefined): Optional. An array of files to be associated with the form data.
|
|
25
|
+
*/
|
|
26
|
+
interface PrepareFormDataParams {
|
|
27
|
+
resourceId: string;
|
|
28
|
+
formId: string;
|
|
29
|
+
content: Record<string, any>;
|
|
30
|
+
files?: File[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Represents prepared form data including the form data object and possible errors.
|
|
34
|
+
*
|
|
35
|
+
* This interface is used for handling form submissions where form data and error messages
|
|
36
|
+
* need to be managed concurrently.
|
|
37
|
+
*
|
|
38
|
+
* @interface
|
|
39
|
+
* @property {FormData} formData - The form data object to be processed or submitted.
|
|
40
|
+
* @property {string[]} [errors] - An optional array of error messages that may be associated
|
|
41
|
+
* with the form data, such as validation issues or processing errors.
|
|
42
|
+
*/
|
|
43
|
+
interface PreparedFormData {
|
|
44
|
+
formData: FormData;
|
|
45
|
+
errors?: string[];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Represents the response for sending a form.
|
|
49
|
+
*
|
|
50
|
+
* The `SendFormResponse` interface is designed to encapsulate the result of a form submission action. It includes information about the success or failure of the submission, any associated data, and a list of error messages if applicable.
|
|
51
|
+
*
|
|
52
|
+
* Properties:
|
|
53
|
+
* - `success` (boolean): Indicates whether the form submission was successful.
|
|
54
|
+
* - `data` (optional): Contains any additional data or payload returned from the submission process. The type of this property is not explicitly defined and can vary based on the implementation.
|
|
55
|
+
* - `errors` (optional): An array of error messages associated with the form submission. This property is present when the submission fails.
|
|
56
|
+
*/
|
|
57
|
+
interface SendFormResponse {
|
|
58
|
+
success: boolean;
|
|
59
|
+
data?: any;
|
|
60
|
+
errors?: string[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A class to interact with the MiraForm API for creating and sending form data.
|
|
65
|
+
*/
|
|
66
|
+
declare class MiraFormSDK {
|
|
67
|
+
private readonly apiKey;
|
|
68
|
+
private options;
|
|
69
|
+
constructor(apiKey: string, options?: MiraFormSDKOptions);
|
|
70
|
+
/**
|
|
71
|
+
* Prepares form data for submission by validating the input parameters and appending them to a FormData object.
|
|
72
|
+
*
|
|
73
|
+
* @param {PrepareFormDataParams} params - The input parameters for preparing the form data.
|
|
74
|
+
* - resourceId: The ID of the resource (required).
|
|
75
|
+
* - formId: The ID of the form (required).
|
|
76
|
+
* - content: The content object to include (optional, must be a valid object).
|
|
77
|
+
* - files: An array of files to include in the form data (optional).
|
|
78
|
+
*
|
|
79
|
+
* @return {PreparedFormData} An object containing the prepared FormData object or error messages if validation fails.
|
|
80
|
+
*/
|
|
81
|
+
prepareFormData(params: PrepareFormDataParams): PreparedFormData;
|
|
82
|
+
/**
|
|
83
|
+
* Sends a form to the specified API endpoint.
|
|
84
|
+
*
|
|
85
|
+
* @param {FormData} formData - The form data to be sent to the server.
|
|
86
|
+
* @return {Promise<SendFormResponse>} A promise that resolves to an object indicating the success of the operation, the server response data, and any errors encountered.
|
|
87
|
+
*/
|
|
88
|
+
sendForm(formData: FormData): Promise<SendFormResponse>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { MiraFormSDK, MiraFormSDKOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for the MiraForm SDK.
|
|
3
|
+
* This interface defines the optional parameters that can be used
|
|
4
|
+
* to customize the behavior of the MiraForm SDK.
|
|
5
|
+
*
|
|
6
|
+
* @property baseUrl - The base URL to which the SDK will send requests. If not provided, a default URL may be used.
|
|
7
|
+
* @property timeout - Specifies the timeout duration (in milliseconds) for requests. If not set, a default timeout may apply.
|
|
8
|
+
*/
|
|
9
|
+
interface MiraFormSDKOptions {
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Represents the parameters required to prepare form data.
|
|
15
|
+
*
|
|
16
|
+
* This interface is used to encapsulate the necessary properties
|
|
17
|
+
* needed to prepare and manage form data, including associated files
|
|
18
|
+
* and form-specific identifiers.
|
|
19
|
+
*
|
|
20
|
+
* Properties:
|
|
21
|
+
* - `resourceId` (string): The unique identifier (UUID) for the resource being processed.
|
|
22
|
+
* - `formId` (string): The unique identifier (UUID) for the form.
|
|
23
|
+
* - `content` (Record<string, any>): A key-value mapping of the form's content data.
|
|
24
|
+
* - `files` (File[] | undefined): Optional. An array of files to be associated with the form data.
|
|
25
|
+
*/
|
|
26
|
+
interface PrepareFormDataParams {
|
|
27
|
+
resourceId: string;
|
|
28
|
+
formId: string;
|
|
29
|
+
content: Record<string, any>;
|
|
30
|
+
files?: File[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Represents prepared form data including the form data object and possible errors.
|
|
34
|
+
*
|
|
35
|
+
* This interface is used for handling form submissions where form data and error messages
|
|
36
|
+
* need to be managed concurrently.
|
|
37
|
+
*
|
|
38
|
+
* @interface
|
|
39
|
+
* @property {FormData} formData - The form data object to be processed or submitted.
|
|
40
|
+
* @property {string[]} [errors] - An optional array of error messages that may be associated
|
|
41
|
+
* with the form data, such as validation issues or processing errors.
|
|
42
|
+
*/
|
|
43
|
+
interface PreparedFormData {
|
|
44
|
+
formData: FormData;
|
|
45
|
+
errors?: string[];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Represents the response for sending a form.
|
|
49
|
+
*
|
|
50
|
+
* The `SendFormResponse` interface is designed to encapsulate the result of a form submission action. It includes information about the success or failure of the submission, any associated data, and a list of error messages if applicable.
|
|
51
|
+
*
|
|
52
|
+
* Properties:
|
|
53
|
+
* - `success` (boolean): Indicates whether the form submission was successful.
|
|
54
|
+
* - `data` (optional): Contains any additional data or payload returned from the submission process. The type of this property is not explicitly defined and can vary based on the implementation.
|
|
55
|
+
* - `errors` (optional): An array of error messages associated with the form submission. This property is present when the submission fails.
|
|
56
|
+
*/
|
|
57
|
+
interface SendFormResponse {
|
|
58
|
+
success: boolean;
|
|
59
|
+
data?: any;
|
|
60
|
+
errors?: string[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A class to interact with the MiraForm API for creating and sending form data.
|
|
65
|
+
*/
|
|
66
|
+
declare class MiraFormSDK {
|
|
67
|
+
private readonly apiKey;
|
|
68
|
+
private options;
|
|
69
|
+
constructor(apiKey: string, options?: MiraFormSDKOptions);
|
|
70
|
+
/**
|
|
71
|
+
* Prepares form data for submission by validating the input parameters and appending them to a FormData object.
|
|
72
|
+
*
|
|
73
|
+
* @param {PrepareFormDataParams} params - The input parameters for preparing the form data.
|
|
74
|
+
* - resourceId: The ID of the resource (required).
|
|
75
|
+
* - formId: The ID of the form (required).
|
|
76
|
+
* - content: The content object to include (optional, must be a valid object).
|
|
77
|
+
* - files: An array of files to include in the form data (optional).
|
|
78
|
+
*
|
|
79
|
+
* @return {PreparedFormData} An object containing the prepared FormData object or error messages if validation fails.
|
|
80
|
+
*/
|
|
81
|
+
prepareFormData(params: PrepareFormDataParams): PreparedFormData;
|
|
82
|
+
/**
|
|
83
|
+
* Sends a form to the specified API endpoint.
|
|
84
|
+
*
|
|
85
|
+
* @param {FormData} formData - The form data to be sent to the server.
|
|
86
|
+
* @return {Promise<SendFormResponse>} A promise that resolves to an object indicating the success of the operation, the server response data, and any errors encountered.
|
|
87
|
+
*/
|
|
88
|
+
sendForm(formData: FormData): Promise<SendFormResponse>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { MiraFormSDK, MiraFormSDKOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/sdk.ts
|
|
2
|
+
var MiraFormSDK = class {
|
|
3
|
+
constructor(apiKey, options) {
|
|
4
|
+
this.apiKey = apiKey;
|
|
5
|
+
this.options = options || {};
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Prepares form data for submission by validating the input parameters and appending them to a FormData object.
|
|
9
|
+
*
|
|
10
|
+
* @param {PrepareFormDataParams} params - The input parameters for preparing the form data.
|
|
11
|
+
* - resourceId: The ID of the resource (required).
|
|
12
|
+
* - formId: The ID of the form (required).
|
|
13
|
+
* - content: The content object to include (optional, must be a valid object).
|
|
14
|
+
* - files: An array of files to include in the form data (optional).
|
|
15
|
+
*
|
|
16
|
+
* @return {PreparedFormData} An object containing the prepared FormData object or error messages if validation fails.
|
|
17
|
+
*/
|
|
18
|
+
prepareFormData(params) {
|
|
19
|
+
const errors = [];
|
|
20
|
+
if (!params.resourceId)
|
|
21
|
+
errors.push("resourceId is required");
|
|
22
|
+
if (!params.formId)
|
|
23
|
+
errors.push("formId is required");
|
|
24
|
+
if (params.content && typeof params.content !== "object")
|
|
25
|
+
errors.push("content must be a valid object");
|
|
26
|
+
if (errors.length)
|
|
27
|
+
return { formData: null, errors };
|
|
28
|
+
const formData = new FormData();
|
|
29
|
+
formData.append("resourceId", params.resourceId);
|
|
30
|
+
formData.append("formId", params.formId);
|
|
31
|
+
formData.append("content", JSON.stringify(params.content));
|
|
32
|
+
if (params.files) {
|
|
33
|
+
params.files.forEach((file, idx) => {
|
|
34
|
+
formData.append(`file_${idx}`, file);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return { formData };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Sends a form to the specified API endpoint.
|
|
41
|
+
*
|
|
42
|
+
* @param {FormData} formData - The form data to be sent to the server.
|
|
43
|
+
* @return {Promise<SendFormResponse>} A promise that resolves to an object indicating the success of the operation, the server response data, and any errors encountered.
|
|
44
|
+
*/
|
|
45
|
+
async sendForm(formData) {
|
|
46
|
+
const url = `${this.options.baseUrl}/api/v1/send`;
|
|
47
|
+
try {
|
|
48
|
+
const res = await fetch(url, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: {
|
|
51
|
+
"Authentication": `Bearer ${this.apiKey}`
|
|
52
|
+
},
|
|
53
|
+
body: formData
|
|
54
|
+
});
|
|
55
|
+
const data = await res.json();
|
|
56
|
+
return { success: res.ok, data, errors: !res.ok ? [data?.message || "Unknown error"] : void 0 };
|
|
57
|
+
} catch (e) {
|
|
58
|
+
return { success: false, errors: [e.message] };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
export {
|
|
63
|
+
MiraFormSDK
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/sdk.ts"],"sourcesContent":["import { MiraFormSDKOptions } from './types';\nimport {\n PrepareFormDataParams,\n PreparedFormData,\n SendFormResponse,\n} from './types';\n\n\n/**\n * A class to interact with the MiraForm API for creating and sending form data.\n */\nexport class MiraFormSDK {\n private readonly apiKey: string;\n private options: MiraFormSDKOptions;\n\n constructor(apiKey: string, options?: MiraFormSDKOptions) {\n this.apiKey = apiKey;\n this.options = options || {};\n }\n\n\n /**\n * Prepares form data for submission by validating the input parameters and appending them to a FormData object.\n *\n * @param {PrepareFormDataParams} params - The input parameters for preparing the form data.\n * - resourceId: The ID of the resource (required).\n * - formId: The ID of the form (required).\n * - content: The content object to include (optional, must be a valid object).\n * - files: An array of files to include in the form data (optional).\n *\n * @return {PreparedFormData} An object containing the prepared FormData object or error messages if validation fails.\n */\n public prepareFormData(params: PrepareFormDataParams): PreparedFormData {\n const errors: string[] = [];\n if (!params.resourceId) errors.push('resourceId is required');\n if (!params.formId) errors.push('formId is required');\n if (params.content && typeof params.content !== 'object') errors.push('content must be a valid object');\n if (errors.length) return { formData: null as any, errors };\n\n const formData = new FormData();\n formData.append('resourceId', params.resourceId);\n formData.append('formId', params.formId);\n formData.append('content', JSON.stringify(params.content));\n if (params.files) {\n params.files.forEach((file, idx) => {\n formData.append(`file_${idx}`, file as any);\n });\n }\n return { formData };\n }\n\n /**\n * Sends a form to the specified API endpoint.\n *\n * @param {FormData} formData - The form data to be sent to the server.\n * @return {Promise<SendFormResponse>} A promise that resolves to an object indicating the success of the operation, the server response data, and any errors encountered.\n */\n public async sendForm(formData: FormData): Promise<SendFormResponse> {\n const url = `${this.options.baseUrl}/api/v1/send`;\n try {\n const res = await fetch(url, {\n method: 'POST',\n headers: {\n 'Authentication': `Bearer ${this.apiKey}`,\n },\n body: formData,\n });\n const data = await res.json();\n return { success: res.ok, data, errors: !res.ok ? [data?.message || 'Unknown error'] : undefined };\n } catch (e: any) {\n return { success: false, errors: [e.message] };\n }\n }\n}\n"],"mappings":";AAWO,IAAM,cAAN,MAAkB;AAAA,EAIvB,YAAY,QAAgB,SAA8B;AACxD,SAAK,SAAS;AACd,SAAK,UAAU,WAAW,CAAC;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcO,gBAAgB,QAAiD;AACtE,UAAM,SAAmB,CAAC;AAC1B,QAAI,CAAC,OAAO;AAAY,aAAO,KAAK,wBAAwB;AAC5D,QAAI,CAAC,OAAO;AAAQ,aAAO,KAAK,oBAAoB;AACpD,QAAI,OAAO,WAAW,OAAO,OAAO,YAAY;AAAU,aAAO,KAAK,gCAAgC;AACtG,QAAI,OAAO;AAAQ,aAAO,EAAE,UAAU,MAAa,OAAO;AAE1D,UAAM,WAAW,IAAI,SAAS;AAC9B,aAAS,OAAO,cAAc,OAAO,UAAU;AAC/C,aAAS,OAAO,UAAU,OAAO,MAAM;AACvC,aAAS,OAAO,WAAW,KAAK,UAAU,OAAO,OAAO,CAAC;AACzD,QAAI,OAAO,OAAO;AAChB,aAAO,MAAM,QAAQ,CAAC,MAAM,QAAQ;AAClC,iBAAS,OAAO,QAAQ,GAAG,IAAI,IAAW;AAAA,MAC5C,CAAC;AAAA,IACH;AACA,WAAO,EAAE,SAAS;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAa,SAAS,UAA+C;AACnE,UAAM,MAAM,GAAG,KAAK,QAAQ,OAAO;AACnC,QAAI;AACF,YAAM,MAAM,MAAM,MAAM,KAAK;AAAA,QAC3B,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,kBAAkB,UAAU,KAAK,MAAM;AAAA,QACzC;AAAA,QACA,MAAM;AAAA,MACR,CAAC;AACD,YAAM,OAAO,MAAM,IAAI,KAAK;AAC5B,aAAO,EAAE,SAAS,IAAI,IAAI,MAAM,QAAQ,CAAC,IAAI,KAAK,CAAC,MAAM,WAAW,eAAe,IAAI,OAAU;AAAA,IACnG,SAAS,GAAQ;AACf,aAAO,EAAE,SAAS,OAAO,QAAQ,CAAC,EAAE,OAAO,EAAE;AAAA,IAC/C;AAAA,EACF;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mira-form-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Mira Form API SDK",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.cjs",
|
|
12
|
+
"import": "./dist/index.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup",
|
|
17
|
+
"prepack": "npm run build",
|
|
18
|
+
"test": "echo \"Error: no test specified\" > exit 1"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"sdk",
|
|
22
|
+
"mira-form",
|
|
23
|
+
"api",
|
|
24
|
+
"typescript"
|
|
25
|
+
],
|
|
26
|
+
"author": "Your Name <artem@miraspaces.com>",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/your-username/mira-form-sdk.git"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/your-username/mira-form-sdk/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/your-username/mira-form-sdk#readme",
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"README.md",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">20"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"tsup": "^7.2.0",
|
|
49
|
+
"typescript": "^5.0.0"
|
|
50
|
+
}
|
|
51
|
+
}
|