@rowslint/importer-js 0.0.1
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 +261 -0
- package/dist/models/importer.model.d.ts +38 -0
- package/dist/rowslint.js +175 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# @rowslint/importer-js
|
|
2
|
+
|
|
3
|
+
The Rowslint JavaScript script provides access to the prebuilt importer component. The package interacts directly with the Rowslint APIs using your API key.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Include a script tag with the import statement pointing to the CDN URL where the `@rowslint/importer-js` package is hosted:
|
|
8
|
+
|
|
9
|
+
```jsx
|
|
10
|
+
<script src="https://cdn.jsdelivr.net/npm/@rowslint/importer-js@latest/dist/rowslint.js"></script>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Import TypeScript interfaces from the `@rowslint/importer-js/dist/models/importer.model` path if you are using TypeScript:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { RowslintConfig } from '@rowslint/importer-js/dist/models/importer.model';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Call the `launch()` method with the organization API key and the template key parameters to display the importer UI.
|
|
20
|
+
|
|
21
|
+
```jsx
|
|
22
|
+
<script>
|
|
23
|
+
const launch = () => {
|
|
24
|
+
rowslint.launch({
|
|
25
|
+
// Your organization API key here.
|
|
26
|
+
apiKey: "ORGANIZATION_API_KEY",
|
|
27
|
+
config: {
|
|
28
|
+
// Your template key here.
|
|
29
|
+
templateKey: "TEMPLATE_KEY"
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
</script>
|
|
34
|
+
|
|
35
|
+
<button onclick="launch()">Launch</button>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
#### Headless UI
|
|
39
|
+
|
|
40
|
+
You can also use your custom upload UI and then use Rowslint importer only to format and validate data (this will display the import modal without the first upload step). To do so, call the `launch()` method with the uploaded XLSX or CSV file (in `File` type).
|
|
41
|
+
|
|
42
|
+
```jsx
|
|
43
|
+
rowslint.launch({
|
|
44
|
+
apiKey: 'ORGANIZATION_API_KEY',
|
|
45
|
+
config: {
|
|
46
|
+
templateKey: 'TEMPLATE_KEY',
|
|
47
|
+
// `inputFile` in `file` type.
|
|
48
|
+
file: inputFile,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Events
|
|
54
|
+
|
|
55
|
+
The `launch()` method provide a callback parameter which is triggered on the importer modal closes. You can use it to handle the closing of the importer after the import has been completed.
|
|
56
|
+
|
|
57
|
+
```jsx
|
|
58
|
+
rowslint.launch({
|
|
59
|
+
apiKey: 'ORGANIZATION_API_KEY',
|
|
60
|
+
config: { templateKey: 'TEMPLATE_KEY' },
|
|
61
|
+
onImport: (result) => {
|
|
62
|
+
switch (result.status) {
|
|
63
|
+
case 'success':
|
|
64
|
+
// Handle spreadsheet import success.
|
|
65
|
+
break;
|
|
66
|
+
case 'error':
|
|
67
|
+
// Handle spreadsheet import error.
|
|
68
|
+
break;
|
|
69
|
+
case 'cancelled':
|
|
70
|
+
// Handle spreadsheet import cancel.
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Parameters
|
|
78
|
+
|
|
79
|
+
The `launch()` method take one object parameter with 4 properties:
|
|
80
|
+
|
|
81
|
+
| Name | Type | Required | Description |
|
|
82
|
+
| -------- | ---------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------- |
|
|
83
|
+
| apiKey | `string` | required | The organization API key. Can be found in the organization page. |
|
|
84
|
+
| config | `RowslintTemplateConfig` | required | Represents the configuration of each template importer. |
|
|
85
|
+
| file | `File` | optional | The uploaded XLSX or CSV file. Used to display the importer modal in the headless mode without the first upload step. |
|
|
86
|
+
| onImport | `(result: RowslintImportResult) => void` | optional | A callback that represents the return of the importer after the modal is closed. |
|
|
87
|
+
|
|
88
|
+
### `RowslintTemplateConfig`
|
|
89
|
+
|
|
90
|
+
#### Description
|
|
91
|
+
|
|
92
|
+
Represents the configuration of each template importer.
|
|
93
|
+
|
|
94
|
+
#### Properties
|
|
95
|
+
|
|
96
|
+
| name | type | required | default | description |
|
|
97
|
+
| ---------------- | --------------------------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
98
|
+
| templateKey | `string` | required | | The template key. Can be found in the template detail/edit page. |
|
|
99
|
+
| language | `'en' \| 'fr'` | optional | 'en' | The language to be used in the importer. |
|
|
100
|
+
| returnType | `'json' \| 'xslx' \| 'csv'` | optional | 'json' | the data format that the import will return. |
|
|
101
|
+
| metadata | `unknown` | optional | | Additional data if you would like to send specific data to your server according to the configuration of the destination template (e.g. logged-in user data: `user_id`...). Can hold any type of data. |
|
|
102
|
+
| customValidators | `RowslintTemplateCustomValidator` | optional | | Object to handle custom validations from your code. |
|
|
103
|
+
|
|
104
|
+
### `RowslintImportResult`
|
|
105
|
+
|
|
106
|
+
#### Description
|
|
107
|
+
|
|
108
|
+
Represents the return of the importer after the modal is closed.
|
|
109
|
+
|
|
110
|
+
#### Properties
|
|
111
|
+
|
|
112
|
+
| name | type | required | default | description |
|
|
113
|
+
| -------- | --------------------------------------------------------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
114
|
+
| status | `'success' \| 'error' \| 'cancelled'` | required | | `'success'` if import completed successfully, else `'error'`. `'cancelled'` if the user quits without completing the import steps. |
|
|
115
|
+
| data | `{ file?: File; rows?: Array<Record<string, unknown>>; }` | optional | | Return the imported file in the `file` property or the JSON data format in the `row` property according to the `returnType` property of the `RowslintTemplateConfig` interface. |
|
|
116
|
+
| metadata | `{ is_valid: boolean; file_name: string; sheet_name: string; }` | optional | | Holds the uploaded file information and the `is_valide` propperty which is `true` when the file is imported respecting all template validations, otherwise `else`. |
|
|
117
|
+
|
|
118
|
+
## Examples
|
|
119
|
+
|
|
120
|
+
### Importer UI
|
|
121
|
+
|
|
122
|
+
This example demonstrates how to use the importer by displaying the importer UI and listening on the event when the import process is complete.
|
|
123
|
+
|
|
124
|
+
```jsx
|
|
125
|
+
<body>
|
|
126
|
+
<script src="https://cdn.jsdelivr.net/npm/@rowslint/importer-js@latest/dist/rowslint.js"></script>
|
|
127
|
+
<script>
|
|
128
|
+
const launch = () => {
|
|
129
|
+
rowslint.launch({
|
|
130
|
+
apiKey: 'ORGANIZATION_API_KEY',
|
|
131
|
+
config: {
|
|
132
|
+
templateKey: 'TEMPLATE_KEY',
|
|
133
|
+
returnType: 'json'
|
|
134
|
+
},
|
|
135
|
+
onImport: (result) => {
|
|
136
|
+
if (result.status === 'success' && result.metadata?.is_valid) {
|
|
137
|
+
// Continue the import process.
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
</script>
|
|
143
|
+
|
|
144
|
+
<button onclick="launch()">Launch</button>
|
|
145
|
+
</body>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Headless importer UI
|
|
149
|
+
|
|
150
|
+
In this example, we will use the importer in the headless mode without the first upload step. To do so, we will use our custom file input field to upload the file, then we will set it to the importer.
|
|
151
|
+
|
|
152
|
+
```jsx
|
|
153
|
+
<body>
|
|
154
|
+
<script src="https://cdn.jsdelivr.net/npm/@rowslint/importer-js@latest/dist/rowslint.js"></script>
|
|
155
|
+
<script>
|
|
156
|
+
const launch = () => {
|
|
157
|
+
const inputFile = document.getElementById('file');
|
|
158
|
+
rowslint.launch({
|
|
159
|
+
apiKey: 'ORGANIZATION_API_KEY',
|
|
160
|
+
config: {
|
|
161
|
+
templateKey: 'TEMPLATE_KEY',
|
|
162
|
+
returnType: 'xslx',
|
|
163
|
+
},
|
|
164
|
+
file: inputFile.files[0],
|
|
165
|
+
onImport: (result) => {
|
|
166
|
+
// Continue the import process.
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
</script>
|
|
171
|
+
|
|
172
|
+
<input id="file" type="file" />
|
|
173
|
+
<button onclick="launch()">Launch</button>
|
|
174
|
+
</body>
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Custom validations
|
|
178
|
+
|
|
179
|
+
We can define a custom configuration of validations from the code. Let's say our template has a "firstname" column. In this example, we will customize the validation of this column to accept only values starting with the letter "A".
|
|
180
|
+
|
|
181
|
+
```jsx
|
|
182
|
+
<body>
|
|
183
|
+
<script src="https://cdn.jsdelivr.net/npm/@rowslint/importer-js@latest/dist/rowslint.js"></script>
|
|
184
|
+
<script>
|
|
185
|
+
const launch = () => {
|
|
186
|
+
rowslint.launch({
|
|
187
|
+
apiKey: 'ORGANIZATION_API_KEY',
|
|
188
|
+
config: {
|
|
189
|
+
templateKey: 'TEMPLATE_KEY'
|
|
190
|
+
},
|
|
191
|
+
customValidators: {
|
|
192
|
+
'firstname': (columnValue) => {
|
|
193
|
+
if (typeof columnValue === 'string' && columnValue.startsWith('A')) {
|
|
194
|
+
// Return `true` if the value is valid.
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
// Return custom message if the value is invalid.
|
|
198
|
+
return {
|
|
199
|
+
message: 'Must start with the letter "A".',
|
|
200
|
+
};
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
onImport: (result) => {
|
|
204
|
+
// Continue the import process.
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
</script>
|
|
209
|
+
|
|
210
|
+
<button onclick="launch()">Launch</button>
|
|
211
|
+
</body>
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Note that:
|
|
215
|
+
|
|
216
|
+
- `firstname` property is the column name and must already be added to the template columns.
|
|
217
|
+
- By defining a column custom validation, this will override the column validation type already defined in the template edition page.
|
|
218
|
+
|
|
219
|
+
We can also return a list of valid values to be displayed in a select field.
|
|
220
|
+
|
|
221
|
+
```jsx
|
|
222
|
+
<body>
|
|
223
|
+
<script src="https://cdn.jsdelivr.net/npm/@rowslint/importer-js@latest/dist/rowslint.js"></script>
|
|
224
|
+
<script>
|
|
225
|
+
const validValues = ['A', 'B'];
|
|
226
|
+
const launch = () => {
|
|
227
|
+
rowslint.launch({
|
|
228
|
+
apiKey: 'ORGANIZATION_API_KEY',
|
|
229
|
+
config: {
|
|
230
|
+
templateKey: 'TEMPLATE_KEY'
|
|
231
|
+
},
|
|
232
|
+
customValidators: {
|
|
233
|
+
'firstname': (columnValue) => {
|
|
234
|
+
if (typeof columnValue === 'string' && validValues.includes(columnValue)) {
|
|
235
|
+
// Return `true` if the value is valid.
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
// Return custom message if the value is invalid.
|
|
239
|
+
return {
|
|
240
|
+
message: 'Must be "A" or "B".',
|
|
241
|
+
validationType: 'choiceList',
|
|
242
|
+
validationOptions: {
|
|
243
|
+
list: validValues,
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
onImport: (result) => {
|
|
249
|
+
// Continue the import process.
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
};
|
|
253
|
+
</script>
|
|
254
|
+
|
|
255
|
+
<button onclick="launch()">Launch</button>
|
|
256
|
+
</body>
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Documentation
|
|
260
|
+
|
|
261
|
+
To see the latest documentation, [please click here](https://docs.rowslint.dev)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export interface RowslintConfig {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
config: RowslintTemplateConfig;
|
|
4
|
+
file?: File;
|
|
5
|
+
onImport?: (result: RowslintImportResult) => void;
|
|
6
|
+
}
|
|
7
|
+
export interface RowslintTemplateConfig {
|
|
8
|
+
readonly templateKey: string;
|
|
9
|
+
language?: 'en' | 'fr';
|
|
10
|
+
returnType?: 'json' | 'xlsx' | 'csv';
|
|
11
|
+
metadata?: unknown;
|
|
12
|
+
customValidators?: RowslintTemplateCustomValidator;
|
|
13
|
+
}
|
|
14
|
+
export type RowslintTemplateCustomValidator = Record<string, (columnValue: unknown) => {
|
|
15
|
+
message?: string;
|
|
16
|
+
validationType?: 'choiceList' | 'text';
|
|
17
|
+
validationOptions?: {
|
|
18
|
+
list: Array<string | number>;
|
|
19
|
+
};
|
|
20
|
+
} | true>;
|
|
21
|
+
export interface RowslintImportResult {
|
|
22
|
+
status: RowslintImportResultStatus;
|
|
23
|
+
data?: {
|
|
24
|
+
file?: File;
|
|
25
|
+
rows?: Array<Record<string, unknown>>;
|
|
26
|
+
};
|
|
27
|
+
metadata?: {
|
|
28
|
+
is_valid: boolean;
|
|
29
|
+
file_name: string;
|
|
30
|
+
sheet_name?: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
declare enum RowslintImportResultStatus {
|
|
34
|
+
SUCCESS = "success",
|
|
35
|
+
ERROR = "error",
|
|
36
|
+
CANCELLED = "cancelled"
|
|
37
|
+
}
|
|
38
|
+
export {};
|