@betterinternship/core 1.2.73 → 1.2.75

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.
@@ -1,89 +1,89 @@
1
- import { ZodType } from 'zod';
2
- import { ClientField } from './fields.client';
3
- import { ServerField } from './fields.server';
4
- export declare const SCHEMA_VERSION = 0;
5
- export declare const PARTIES: string[];
6
- export declare const SOURCES: string[];
7
- export declare const FIELD_TYPES: string[];
8
- export interface IFormMetadata {
9
- name: string;
10
- label: string;
11
- schema_version: number;
12
- schema: IFormField[];
13
- schema_phantoms?: IFormPhantomField[];
14
- subscribers: IFormSubscriber[];
15
- signatories: IFormSignatory[];
16
- required_parties: ('university' | 'entity' | 'student' | 'student-guardian')[];
17
- params?: IFormParams;
18
- }
19
- export interface IFormParams {
20
- [key: string]: string | IFormParams;
21
- }
22
- interface IFormContact {
23
- name: string;
24
- honorific: string;
25
- title: string;
26
- email: string;
27
- }
28
- export type IFormSubscriber = IFormContact;
29
- export interface IFormSignatory extends IFormContact {
30
- field: string;
31
- }
32
- export interface IFormField {
33
- field: string;
34
- type: 'text' | 'signature';
35
- x: number;
36
- y: number;
37
- w: number;
38
- h: number;
39
- page: number;
40
- align_h?: 'left' | 'center' | 'right';
41
- align_v?: 'top' | 'middle' | 'bottom';
42
- label: string;
43
- source: 'auto' | 'prefill' | 'derived' | 'manual';
44
- party: 'university' | 'entity' | 'student' | 'student-guardian';
45
- shared: boolean;
46
- descriptor?: boolean;
47
- tooltip_label: string;
48
- validator: string;
49
- prefiller: string;
50
- }
51
- export interface IFormPhantomField {
52
- field: string;
53
- label: string;
54
- type: 'email' | 'param';
55
- source: 'auto' | 'prefill' | 'derived' | 'manual';
56
- party: 'university' | 'entity' | 'student' | 'student-guardian';
57
- shared: boolean;
58
- descriptor?: boolean;
59
- tooltip_label: string;
60
- validator: string;
61
- prefiller: string;
62
- }
63
- export type IFormSourceDomains<SourceDomains extends any[]> = Record<string, SourceDomains[number]>;
64
- export type IFormFieldValidator = ZodType;
65
- export type IFormFieldPrefiller<SourceDomains extends any[]> = (sourceDomains: IFormSourceDomains<SourceDomains>) => string;
66
- export declare class FormMetadata<SourceDomains extends any[]> {
67
- private readonly formMetadata;
68
- private readonly fields;
69
- constructor(formMetadata: IFormMetadata);
70
- encodeAsJSON(): string;
71
- static decodeFromJSON<SourceDomains extends any[]>(json: string): FormMetadata<SourceDomains>;
72
- getLabel(): string;
73
- getFieldForClient(index: number, sourceDomains?: IFormSourceDomains<SourceDomains>, derivationBase?: IFormParams): ClientField<SourceDomains>;
74
- getFieldsForClient(sourceDomains?: IFormSourceDomains<SourceDomains>, derivationBase?: IFormParams): ClientField<SourceDomains>[];
75
- getFieldsForServer(): ServerField[];
76
- getPhantomFields(): IFormPhantomField[];
77
- inferParams(): string[];
78
- getParams(): IFormParams;
79
- getDescriptors(): IFormField[];
80
- getSignatoryAsValues(): Record<string, string>;
81
- private parseValidator;
82
- private parsePrefiller;
83
- private enumerateParams;
84
- private populateParams;
85
- getSignatories(): Omit<IFormContact, 'honorific'>[];
86
- getSubscribers(): Omit<IFormContact, 'honorific'>[];
87
- getRequiredParties(): string[];
88
- }
89
- export {};
1
+ import { ZodType } from 'zod';
2
+ import { ClientField } from './fields.client';
3
+ import { ServerField } from './fields.server';
4
+ export declare const SCHEMA_VERSION = 0;
5
+ export declare const PARTIES: string[];
6
+ export declare const SOURCES: string[];
7
+ export declare const FIELD_TYPES: string[];
8
+ export interface IFormMetadata {
9
+ name: string;
10
+ label: string;
11
+ schema_version: number;
12
+ schema: IFormField[];
13
+ schema_phantoms?: IFormPhantomField[];
14
+ subscribers: IFormSubscriber[];
15
+ signatories: IFormSignatory[];
16
+ required_parties: ('university' | 'entity' | 'student' | 'student-guardian')[];
17
+ params?: IFormParams;
18
+ }
19
+ export interface IFormParams {
20
+ [key: string]: string | IFormParams;
21
+ }
22
+ interface IFormContact {
23
+ name: string;
24
+ honorific: string;
25
+ title: string;
26
+ email: string;
27
+ }
28
+ export type IFormSubscriber = IFormContact;
29
+ export interface IFormSignatory extends IFormContact {
30
+ field: string;
31
+ }
32
+ export interface IFormField {
33
+ field: string;
34
+ type: 'text' | 'signature';
35
+ x: number;
36
+ y: number;
37
+ w: number;
38
+ h: number;
39
+ page: number;
40
+ align_h?: 'left' | 'center' | 'right';
41
+ align_v?: 'top' | 'middle' | 'bottom';
42
+ label: string;
43
+ source: 'auto' | 'prefill' | 'derived' | 'manual';
44
+ party: 'university' | 'entity' | 'student' | 'student-guardian';
45
+ shared: boolean;
46
+ descriptor?: boolean;
47
+ tooltip_label: string;
48
+ validator: string;
49
+ prefiller: string;
50
+ }
51
+ export interface IFormPhantomField {
52
+ field: string;
53
+ label: string;
54
+ type: 'email' | 'param';
55
+ source: 'auto' | 'prefill' | 'derived' | 'manual';
56
+ party: 'university' | 'entity' | 'student' | 'student-guardian';
57
+ shared: boolean;
58
+ descriptor?: boolean;
59
+ tooltip_label: string;
60
+ validator: string;
61
+ prefiller: string;
62
+ }
63
+ export type IFormSourceDomains<SourceDomains extends any[]> = Record<string, SourceDomains[number]>;
64
+ export type IFormFieldValidator = ZodType;
65
+ export type IFormFieldPrefiller<SourceDomains extends any[]> = (sourceDomains: IFormSourceDomains<SourceDomains>) => string;
66
+ export declare class FormMetadata<SourceDomains extends any[]> {
67
+ private readonly formMetadata;
68
+ private readonly fields;
69
+ constructor(formMetadata: IFormMetadata);
70
+ encodeAsJSON(): string;
71
+ static decodeFromJSON<SourceDomains extends any[]>(json: string): FormMetadata<SourceDomains>;
72
+ getLabel(): string;
73
+ getFieldForClient(index: number, sourceDomains?: IFormSourceDomains<SourceDomains>, derivationBase?: IFormParams): ClientField<SourceDomains>;
74
+ getFieldsForClient(sourceDomains?: IFormSourceDomains<SourceDomains>, derivationBase?: IFormParams): ClientField<SourceDomains>[];
75
+ getFieldsForServer(): ServerField[];
76
+ getPhantomFields(): IFormPhantomField[];
77
+ inferParams(): string[];
78
+ getParams(): IFormParams;
79
+ getDescriptors(): IFormField[];
80
+ getSignatoryAsValues(): Record<string, string>;
81
+ private parseValidator;
82
+ private parsePrefiller;
83
+ private enumerateParams;
84
+ private populateParams;
85
+ getSignatories(): IFormContact[];
86
+ getSubscribers(): IFormContact[];
87
+ getRequiredParties(): string[];
88
+ }
89
+ export {};
@@ -1,227 +1,224 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', { value: true });
3
- exports.FormMetadata =
4
- exports.FIELD_TYPES =
5
- exports.SOURCES =
6
- exports.PARTIES =
7
- exports.SCHEMA_VERSION =
8
- void 0;
9
- const zod_1 = require('zod');
10
- const fields_client_1 = require('./fields.client');
11
- exports.SCHEMA_VERSION = 0;
12
- exports.PARTIES = ['student', 'student-guardian', 'university', 'entity'];
13
- exports.SOURCES = ['auto', 'prefill', 'derived', 'manual'];
14
- exports.FIELD_TYPES = ['text', 'signature'];
15
- class FormMetadata {
16
- formMetadata;
17
- fields;
18
- constructor(formMetadata) {
19
- this.formMetadata = formMetadata;
20
- this.fields = formMetadata.schema;
21
- }
22
- encodeAsJSON() {
23
- return JSON.stringify(this.formMetadata);
24
- }
25
- static decodeFromJSON(json) {
26
- return JSON.parse(json);
27
- }
28
- getLabel() {
29
- return this.formMetadata.label;
30
- }
31
- getFieldForClient(index, sourceDomains, derivationBase) {
32
- const field = this.fields[index];
33
- if (!field) throw new Error('Field does not exist');
34
- const finalParams = {
35
- ...derivationBase,
36
- ...sourceDomains,
37
- ...this.formMetadata.params,
38
- };
39
- const validator = this.parseValidator(field.field, finalParams);
40
- const prefiller = this.parsePrefiller(field.field, finalParams);
41
- const section = field.field.split('.')[0];
42
- let options = undefined;
43
- if (validator?.type === 'enum') options = validator.options;
44
- if (validator?.type === 'array') {
45
- const element = validator.element;
46
- if (element?.options) options = element.options;
47
- }
48
- const type =
49
- field.type === 'signature'
50
- ? 'signature'
51
- : validator
52
- ? (0, fields_client_1.getSchemaClientType)(validator)
53
- : 'text';
54
- const coerce = (value) => {
55
- switch (type) {
56
- case 'number': {
57
- const n = parseInt(value);
58
- return isNaN(n) ? 0 : n;
59
- }
60
- case 'date': {
61
- const n = parseInt(value);
62
- return isNaN(n) ? new Date() : new Date(n);
63
- }
64
- case 'checkbox': {
65
- return !!value?.trim();
66
- }
67
- case 'multiselect': {
68
- return value?.split('\n');
69
- }
70
- default:
71
- return value;
72
- }
73
- };
74
- return {
75
- field: field.field,
76
- shared: field.shared,
77
- label: field.label,
78
- source: field.source,
79
- party: field.party,
80
- tooltip_label: field.tooltip_label,
81
- descriptor: field.descriptor,
82
- coerce,
83
- type,
84
- section,
85
- validator,
86
- prefiller,
87
- options,
88
- };
89
- }
90
- getFieldsForClient(sourceDomains, derivationBase) {
91
- return this.fields
92
- .map((field, i) =>
93
- this.getFieldForClient(i, sourceDomains, derivationBase),
94
- )
95
- .filter((field) => field !== null);
96
- }
97
- getFieldsForServer() {
98
- return this.fields
99
- .map((field) => ({
100
- field: field.field,
101
- type: field.type,
102
- x: field.x,
103
- y: field.y,
104
- w: field.w,
105
- h: field.h,
106
- align_h: field.align_h ?? 'center',
107
- align_v: field.align_v ?? 'bottom',
108
- page: field.page,
109
- }))
110
- .filter((field) => field !== null);
111
- }
112
- getPhantomFields() {
113
- return this.formMetadata.schema_phantoms ?? [];
114
- }
115
- inferParams() {
116
- const inferenceRegex = /params\[\s*(['"])(.*?)\1\s*\]/g;
117
- const params = [];
118
- let match;
119
- for (const field of this.formMetadata.schema) {
120
- while ((match = inferenceRegex.exec(field.validator)) !== null)
121
- if (!params.includes(match[2])) params.push(match[2]);
122
- while ((match = inferenceRegex.exec(field.prefiller)) !== null)
123
- if (!params.includes(match[2])) params.push(match[2]);
124
- }
125
- for (const field of this.formMetadata.schema_phantoms ?? []) {
126
- while ((match = inferenceRegex.exec(field.validator)) !== null)
127
- if (!params.includes(match[2])) params.push(match[2]);
128
- while ((match = inferenceRegex.exec(field.prefiller)) !== null)
129
- if (!params.includes(match[2])) params.push(match[2]);
130
- }
131
- return params;
132
- }
133
- getParams() {
134
- return this.formMetadata.params ?? {};
135
- }
136
- getDescriptors() {
137
- return this.fields.filter((f) => f.descriptor);
138
- }
139
- getSignatoryAsValues() {
140
- const signatories = this.formMetadata.signatories;
141
- const values = {};
142
- for (const signatory of signatories)
143
- values[signatory.field] = signatory.name;
144
- return values;
145
- }
146
- parseValidator(fieldname, params = {}) {
147
- const field = this.fields.find((f) => f.field === fieldname);
148
- if (!field) return null;
149
- const d = new Date();
150
- const defaults = {
151
- currentDate: d,
152
- currentDateTimestamp: d.getTime(),
153
- };
154
- const internalParams = this.formMetadata.params ?? {};
155
- const validator = this.populateParams(field.validator, {
156
- ...params,
157
- ...internalParams,
158
- });
159
- const ret = `return ${validator}`;
160
- const evaluator = new Function('z', 'params', ret);
161
- return evaluator(zod_1.default, { ...params, ...defaults });
162
- }
163
- parsePrefiller(fieldname, params = {}) {
164
- const field = this.fields.find((f) => f.field === fieldname);
165
- if (!field) return null;
166
- const internalParams = this.formMetadata.params ?? {};
167
- const prefiller = this.populateParams(field.prefiller, {
168
- ...params,
169
- ...internalParams,
170
- });
171
- const ret = `return ${prefiller}`;
172
- const evaluator = new Function('params', ret);
173
- return evaluator(params ?? {});
174
- }
175
- enumerateParams(s) {
176
- const detectedParams = s.match(/#\{[^}]*\}/g) ?? [];
177
- const params = detectedParams.map((dp) => {
178
- const [fieldName, propertyTree] = dp
179
- .replaceAll('#{', '')
180
- .replaceAll('}', '')
181
- .replaceAll(')', '')
182
- .split('(');
183
- const properties = propertyTree?.split('.') ?? [];
184
- return [fieldName, ...properties];
185
- });
186
- return params?.reduce(
187
- (acc, cur, i) => ((acc[detectedParams[i]] = cur), acc),
188
- {},
189
- );
190
- }
191
- populateParams(s, params) {
192
- const paramsReflection = this.enumerateParams(s);
193
- const paramsValues = {};
194
- for (const match in paramsReflection) {
195
- const path = paramsReflection[match];
196
- paramsValues[match] = path.reduce((acc, cur) => acc?.[cur], params) ?? '';
197
- }
198
- let out = s;
199
- for (const match in paramsValues) {
200
- const m = paramsValues[match];
201
- const replacement = typeof m === 'string' ? m : JSON.stringify(m);
202
- out = out.replaceAll(match, replacement);
203
- }
204
- return out;
205
- }
206
- getSignatories() {
207
- return this.formMetadata.signatories.map((signatory) => ({
208
- name: `${signatory.name}`,
209
- title: signatory.title,
210
- email: signatory.email,
211
- honorific: signatory.honorific,
212
- }));
213
- }
214
- getSubscribers() {
215
- return this.formMetadata.subscribers.map((subscribers) => ({
216
- name: `${subscribers.name}`,
217
- title: subscribers.title,
218
- email: subscribers.email,
219
- honorific: subscribers.honorific,
220
- }));
221
- }
222
- getRequiredParties() {
223
- return this.formMetadata.required_parties;
224
- }
225
- }
226
- exports.FormMetadata = FormMetadata;
227
- //# sourceMappingURL=form-metadata.js.map
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FormMetadata = exports.FIELD_TYPES = exports.SOURCES = exports.PARTIES = exports.SCHEMA_VERSION = void 0;
4
+ const zod_1 = require("zod");
5
+ const fields_client_1 = require("./fields.client");
6
+ exports.SCHEMA_VERSION = 0;
7
+ exports.PARTIES = ['student', 'student-guardian', 'university', 'entity'];
8
+ exports.SOURCES = ['auto', 'prefill', 'derived', 'manual'];
9
+ exports.FIELD_TYPES = ['text', 'signature'];
10
+ class FormMetadata {
11
+ formMetadata;
12
+ fields;
13
+ constructor(formMetadata) {
14
+ this.formMetadata = formMetadata;
15
+ this.fields = formMetadata.schema;
16
+ }
17
+ encodeAsJSON() {
18
+ return JSON.stringify(this.formMetadata);
19
+ }
20
+ static decodeFromJSON(json) {
21
+ return JSON.parse(json);
22
+ }
23
+ getLabel() {
24
+ return this.formMetadata.label;
25
+ }
26
+ getFieldForClient(index, sourceDomains, derivationBase) {
27
+ const field = this.fields[index];
28
+ if (!field)
29
+ throw new Error('Field does not exist');
30
+ const finalParams = {
31
+ ...derivationBase,
32
+ ...sourceDomains,
33
+ ...this.formMetadata.params,
34
+ };
35
+ const validator = this.parseValidator(field.field, finalParams);
36
+ const prefiller = this.parsePrefiller(field.field, finalParams);
37
+ const section = field.field.split('.')[0];
38
+ let options = undefined;
39
+ if (validator?.type === 'enum')
40
+ options = validator.options;
41
+ if (validator?.type === 'array') {
42
+ const element = validator.element;
43
+ if (element?.options)
44
+ options = element.options;
45
+ }
46
+ const type = field.type === 'signature'
47
+ ? 'signature'
48
+ : validator
49
+ ? (0, fields_client_1.getSchemaClientType)(validator)
50
+ : 'text';
51
+ const coerce = (value) => {
52
+ switch (type) {
53
+ case 'number': {
54
+ const n = parseInt(value);
55
+ return isNaN(n) ? 0 : n;
56
+ }
57
+ case 'date': {
58
+ const n = parseInt(value);
59
+ return isNaN(n) ? new Date() : new Date(n);
60
+ }
61
+ case 'checkbox': {
62
+ return !!value?.trim();
63
+ }
64
+ case 'multiselect': {
65
+ return value?.split('\n');
66
+ }
67
+ default:
68
+ return value;
69
+ }
70
+ };
71
+ return {
72
+ field: field.field,
73
+ shared: field.shared,
74
+ label: field.label,
75
+ source: field.source,
76
+ party: field.party,
77
+ tooltip_label: field.tooltip_label,
78
+ descriptor: field.descriptor,
79
+ coerce,
80
+ type,
81
+ section,
82
+ validator,
83
+ prefiller,
84
+ options,
85
+ };
86
+ }
87
+ getFieldsForClient(sourceDomains, derivationBase) {
88
+ return this.fields
89
+ .map((field, i) => this.getFieldForClient(i, sourceDomains, derivationBase))
90
+ .filter((field) => field !== null);
91
+ }
92
+ getFieldsForServer() {
93
+ return this.fields
94
+ .map((field) => ({
95
+ field: field.field,
96
+ type: field.type,
97
+ x: field.x,
98
+ y: field.y,
99
+ w: field.w,
100
+ h: field.h,
101
+ align_h: field.align_h ?? 'center',
102
+ align_v: field.align_v ?? 'bottom',
103
+ page: field.page,
104
+ }))
105
+ .filter((field) => field !== null);
106
+ }
107
+ getPhantomFields() {
108
+ return this.formMetadata.schema_phantoms ?? [];
109
+ }
110
+ inferParams() {
111
+ const inferenceRegex = /params\[\s*(['"])(.*?)\1\s*\]/g;
112
+ const params = [];
113
+ let match;
114
+ for (const field of this.formMetadata.schema) {
115
+ while ((match = inferenceRegex.exec(field.validator)) !== null)
116
+ if (!params.includes(match[2]))
117
+ params.push(match[2]);
118
+ while ((match = inferenceRegex.exec(field.prefiller)) !== null)
119
+ if (!params.includes(match[2]))
120
+ params.push(match[2]);
121
+ }
122
+ for (const field of this.formMetadata.schema_phantoms ?? []) {
123
+ while ((match = inferenceRegex.exec(field.validator)) !== null)
124
+ if (!params.includes(match[2]))
125
+ params.push(match[2]);
126
+ while ((match = inferenceRegex.exec(field.prefiller)) !== null)
127
+ if (!params.includes(match[2]))
128
+ params.push(match[2]);
129
+ }
130
+ return params;
131
+ }
132
+ getParams() {
133
+ return this.formMetadata.params ?? {};
134
+ }
135
+ getDescriptors() {
136
+ return this.fields.filter((f) => f.descriptor);
137
+ }
138
+ getSignatoryAsValues() {
139
+ const signatories = this.formMetadata.signatories;
140
+ const values = {};
141
+ for (const signatory of signatories)
142
+ values[signatory.field] = signatory.name;
143
+ return values;
144
+ }
145
+ parseValidator(fieldname, params = {}) {
146
+ const field = this.fields.find((f) => f.field === fieldname);
147
+ if (!field)
148
+ return null;
149
+ const d = new Date();
150
+ const defaults = {
151
+ currentDate: d,
152
+ currentDateTimestamp: d.getTime(),
153
+ };
154
+ const internalParams = this.formMetadata.params ?? {};
155
+ const validator = this.populateParams(field.validator, {
156
+ ...params,
157
+ ...internalParams,
158
+ });
159
+ const ret = `return ${validator}`;
160
+ const evaluator = new Function('z', 'params', ret);
161
+ return evaluator(zod_1.default, { ...params, ...defaults });
162
+ }
163
+ parsePrefiller(fieldname, params = {}) {
164
+ const field = this.fields.find((f) => f.field === fieldname);
165
+ if (!field)
166
+ return null;
167
+ const internalParams = this.formMetadata.params ?? {};
168
+ const prefiller = this.populateParams(field.prefiller, {
169
+ ...params,
170
+ ...internalParams,
171
+ });
172
+ const ret = `return ${prefiller}`;
173
+ const evaluator = new Function('params', ret);
174
+ return evaluator(params ?? {});
175
+ }
176
+ enumerateParams(s) {
177
+ const detectedParams = s.match(/#\{[^}]*\}/g) ?? [];
178
+ const params = detectedParams.map((dp) => {
179
+ const [fieldName, propertyTree] = dp
180
+ .replaceAll('#{', '')
181
+ .replaceAll('}', '')
182
+ .replaceAll(')', '')
183
+ .split('(');
184
+ const properties = propertyTree?.split('.') ?? [];
185
+ return [fieldName, ...properties];
186
+ });
187
+ return params?.reduce((acc, cur, i) => ((acc[detectedParams[i]] = cur), acc), {});
188
+ }
189
+ populateParams(s, params) {
190
+ const paramsReflection = this.enumerateParams(s);
191
+ const paramsValues = {};
192
+ for (const match in paramsReflection) {
193
+ const path = paramsReflection[match];
194
+ paramsValues[match] = path.reduce((acc, cur) => acc?.[cur], params) ?? '';
195
+ }
196
+ let out = s;
197
+ for (const match in paramsValues) {
198
+ const replacement = JSON.stringify(paramsValues[match]);
199
+ out = out.replaceAll(match, replacement);
200
+ }
201
+ return out;
202
+ }
203
+ getSignatories() {
204
+ return this.formMetadata.signatories.map((signatory) => ({
205
+ name: signatory.name,
206
+ title: signatory.title,
207
+ email: signatory.email,
208
+ honorific: signatory.honorific,
209
+ }));
210
+ }
211
+ getSubscribers() {
212
+ return this.formMetadata.subscribers.map((subscriber) => ({
213
+ name: subscriber.name,
214
+ title: subscriber.title,
215
+ email: subscriber.email,
216
+ honorific: subscriber.honorific,
217
+ }));
218
+ }
219
+ getRequiredParties() {
220
+ return this.formMetadata.required_parties;
221
+ }
222
+ }
223
+ exports.FormMetadata = FormMetadata;
224
+ //# sourceMappingURL=form-metadata.js.map