@lsync/definitions 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/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/index.d.mts +69 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +118 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 lsync contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type SchemaOutput<TSchema extends StandardSchemaV1> = TSchema extends StandardSchemaV1<any, infer TOutput> ? TOutput : never;
|
|
5
|
+
interface ApiDefinition<TInput = undefined, TOutput = unknown> {
|
|
6
|
+
input?: StandardSchemaV1;
|
|
7
|
+
output?: StandardSchemaV1;
|
|
8
|
+
path?: string;
|
|
9
|
+
readonly _input?: TInput;
|
|
10
|
+
readonly _output?: TOutput;
|
|
11
|
+
}
|
|
12
|
+
type ApiDefinitions = Record<string, ApiDefinition<any, any>>;
|
|
13
|
+
type CollectionDefinitions = Record<string, CollectionDefinition<any, any, any, any>>;
|
|
14
|
+
interface CollectionDefinition<TRow extends object = Record<string, unknown>, TKey extends keyof TRow & string = keyof TRow & string, TChildren extends CollectionDefinitions = CollectionDefinitions, TApi extends ApiDefinitions = ApiDefinitions> {
|
|
15
|
+
name: string;
|
|
16
|
+
path: string;
|
|
17
|
+
key: TKey;
|
|
18
|
+
schema: StandardSchemaV1;
|
|
19
|
+
children: TChildren;
|
|
20
|
+
api: TApi;
|
|
21
|
+
}
|
|
22
|
+
interface CollectionSet<TCollections extends CollectionDefinitions = CollectionDefinitions> {
|
|
23
|
+
collections: TCollections;
|
|
24
|
+
}
|
|
25
|
+
type CollectionPath<TCollections extends CollectionDefinitions> = (keyof TCollections & string) | { [TName in keyof TCollections & string]: `${TName}.${keyof TCollections[TName]["children"] & string}` }[keyof TCollections & string];
|
|
26
|
+
type CollectionAtPath<TCollections extends CollectionDefinitions, TPath extends string> = TPath extends `${infer THead}.${infer TRest}` ? THead extends keyof TCollections & string ? CollectionAtPath<TCollections[THead]["children"], TRest> : never : TPath extends keyof TCollections & string ? TCollections[TPath] : never;
|
|
27
|
+
declare function defineCollections(): CollectionsBuilder<{}>;
|
|
28
|
+
declare class CollectionsBuilder<TCollections extends CollectionDefinitions> {
|
|
29
|
+
private readonly collections;
|
|
30
|
+
constructor(collections: TCollections);
|
|
31
|
+
collection<TName extends string, TBuilder extends CollectionBuilder<any, any, any, any, any>>(name: TName, configure: (collection: CollectionBuilder<TName, Record<string, unknown>, never, {}, {}>) => TBuilder): CollectionsBuilder<TCollections & Record<TName, DefinitionFromBuilder<TBuilder>>>;
|
|
32
|
+
build(): CollectionSet<TCollections> & TCollections;
|
|
33
|
+
rawCollections(): TCollections;
|
|
34
|
+
}
|
|
35
|
+
declare class CollectionBuilder<TName extends string, TRow extends object, TKey extends string, TChildren extends CollectionDefinitions, TApi extends ApiDefinitions> {
|
|
36
|
+
private readonly state;
|
|
37
|
+
constructor(state: CollectionBuilderState);
|
|
38
|
+
schema<TNextSchema extends StandardSchemaV1>(schema: TNextSchema): CollectionBuilder<TName, Extract<SchemaOutput<TNextSchema>, object>, never, TChildren, TApi>;
|
|
39
|
+
key<TKeyNext extends keyof TRow & string>(key: TKeyNext): CollectionBuilder<TName, TRow, TKeyNext, TChildren, TApi>;
|
|
40
|
+
api<TApiName extends string, TBuilder extends ApiBuilder<any, any>>(name: TApiName, configure: (api: ApiBuilder<undefined, unknown>) => TBuilder): CollectionBuilder<TName, TRow, TKey, TChildren, TApi & Record<TApiName, ApiFromBuilder<TBuilder>>>;
|
|
41
|
+
children<TBuilder extends CollectionsBuilder<any>>(configure: (children: CollectionsBuilder<{}>) => TBuilder): CollectionBuilder<TName, TRow, TKey, BuilderCollections<TBuilder>, TApi>;
|
|
42
|
+
toDefinition(parentPath: string, parentName: string | undefined): DefinitionFromBuilder<this>;
|
|
43
|
+
}
|
|
44
|
+
declare class ApiBuilder<TInput, TOutput> {
|
|
45
|
+
private readonly state;
|
|
46
|
+
constructor(state: ApiBuilderState);
|
|
47
|
+
input<TSchema extends StandardSchemaV1>(schema: TSchema): ApiBuilder<SchemaOutput<TSchema>, TOutput>;
|
|
48
|
+
output<TSchema extends StandardSchemaV1>(schema: TSchema): ApiBuilder<TInput, SchemaOutput<TSchema>>;
|
|
49
|
+
path(path: string): ApiBuilder<TInput, TOutput>;
|
|
50
|
+
toDefinition(): ApiDefinition<TInput, TOutput>;
|
|
51
|
+
}
|
|
52
|
+
interface CollectionBuilderState {
|
|
53
|
+
name: string;
|
|
54
|
+
schema?: StandardSchemaV1;
|
|
55
|
+
key?: string;
|
|
56
|
+
children?: CollectionDefinitions;
|
|
57
|
+
api?: ApiDefinitions;
|
|
58
|
+
}
|
|
59
|
+
interface ApiBuilderState {
|
|
60
|
+
input?: StandardSchemaV1;
|
|
61
|
+
output?: StandardSchemaV1;
|
|
62
|
+
path?: string;
|
|
63
|
+
}
|
|
64
|
+
type DefinitionFromBuilder<TBuilder> = TBuilder extends CollectionBuilder<any, infer TRow, infer TKey, infer TChildren, infer TApi> ? CollectionDefinition<TRow, Extract<TKey, keyof TRow & string>, TChildren, TApi> : never;
|
|
65
|
+
type ApiFromBuilder<TBuilder> = TBuilder extends ApiBuilder<infer TInput, infer TOutput> ? ApiDefinition<TInput, TOutput> : never;
|
|
66
|
+
type BuilderCollections<TBuilder> = TBuilder extends CollectionsBuilder<infer TCollections> ? TCollections : never;
|
|
67
|
+
//#endregion
|
|
68
|
+
export { ApiBuilder, ApiDefinition, ApiDefinitions, CollectionAtPath, CollectionBuilder, CollectionDefinition, CollectionDefinitions, CollectionPath, CollectionSet, CollectionsBuilder, SchemaOutput, defineCollections };
|
|
69
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;KAEY,YAAA,iBAA6B,gBAAA,IACvC,OAAA,SAAgB,gBAAA,uBAAuC,OAAA;AAAA,UAExC,aAAA;EACf,KAAA,GAAQ,gBAAA;EACR,MAAA,GAAS,gBAAA;EACT,IAAA;EAAA,SACS,MAAA,GAAS,MAAA;EAAA,SACT,OAAA,GAAU,OAAA;AAAA;AAAA,KAGT,cAAA,GAAiB,MAAM,SAAS,aAAA;AAAA,KAChC,qBAAA,GAAwB,MAAM,SAAS,oBAAA;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IAAA,EAAM,OAAA,CAAQ,IAAA,QAAY,IAAA,YAAgB,SAAA,EAAW,IAAA;AAAA,KAG3E,cAAA,aACH,QAAA,SAAiB,UAAA,gCAA0C,aAAA,CAAc,MAAA,EAAQ,OAAA;AAAA,KAE9E,kBAAA,aACH,QAAA,SAAiB,kBAAkB,uBAAuB,YAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
function defineCollections() {
|
|
3
|
+
return new CollectionsBuilder({});
|
|
4
|
+
}
|
|
5
|
+
var CollectionsBuilder = class CollectionsBuilder {
|
|
6
|
+
collections;
|
|
7
|
+
constructor(collections) {
|
|
8
|
+
this.collections = collections;
|
|
9
|
+
}
|
|
10
|
+
collection(name, configure) {
|
|
11
|
+
const definition = configure(new CollectionBuilder({ name })).toDefinition("", void 0);
|
|
12
|
+
return new CollectionsBuilder({
|
|
13
|
+
...this.collections,
|
|
14
|
+
[name]: definition
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
build() {
|
|
18
|
+
return Object.freeze({
|
|
19
|
+
collections: this.collections,
|
|
20
|
+
...this.collections
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
rawCollections() {
|
|
24
|
+
return this.collections;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var CollectionBuilder = class CollectionBuilder {
|
|
28
|
+
state;
|
|
29
|
+
constructor(state) {
|
|
30
|
+
this.state = state;
|
|
31
|
+
}
|
|
32
|
+
schema(schema) {
|
|
33
|
+
return new CollectionBuilder({
|
|
34
|
+
...this.state,
|
|
35
|
+
schema
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
key(key) {
|
|
39
|
+
return new CollectionBuilder({
|
|
40
|
+
...this.state,
|
|
41
|
+
key
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
api(name, configure) {
|
|
45
|
+
const api = configure(new ApiBuilder({}));
|
|
46
|
+
return new CollectionBuilder({
|
|
47
|
+
...this.state,
|
|
48
|
+
api: {
|
|
49
|
+
...this.state.api,
|
|
50
|
+
[name]: api.toDefinition()
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
children(configure) {
|
|
55
|
+
return new CollectionBuilder({
|
|
56
|
+
...this.state,
|
|
57
|
+
children: configure(defineCollections()).rawCollections()
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
toDefinition(parentPath, parentName) {
|
|
61
|
+
if (!this.state.schema) throw new Error(`Collection ${this.state.name} requires a schema`);
|
|
62
|
+
if (!this.state.key) throw new Error(`Collection ${this.state.name} requires a key`);
|
|
63
|
+
const path = collectionPath(parentPath, parentName, this.state.name);
|
|
64
|
+
const children = Object.fromEntries(Object.entries(this.state.children ?? {}).map(([name, child]) => [name, rebaseDefinition(child, path, this.state.name)]));
|
|
65
|
+
return Object.freeze({
|
|
66
|
+
name: this.state.name,
|
|
67
|
+
path,
|
|
68
|
+
key: this.state.key,
|
|
69
|
+
schema: this.state.schema,
|
|
70
|
+
children,
|
|
71
|
+
api: this.state.api ?? {}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var ApiBuilder = class ApiBuilder {
|
|
76
|
+
state;
|
|
77
|
+
constructor(state) {
|
|
78
|
+
this.state = state;
|
|
79
|
+
}
|
|
80
|
+
input(schema) {
|
|
81
|
+
return new ApiBuilder({
|
|
82
|
+
...this.state,
|
|
83
|
+
input: schema
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
output(schema) {
|
|
87
|
+
return new ApiBuilder({
|
|
88
|
+
...this.state,
|
|
89
|
+
output: schema
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
path(path) {
|
|
93
|
+
return new ApiBuilder({
|
|
94
|
+
...this.state,
|
|
95
|
+
path
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
toDefinition() {
|
|
99
|
+
return Object.freeze({ ...this.state });
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
function collectionPath(parentPath, parentName, name) {
|
|
103
|
+
if (!parentPath) return `/${encodeURIComponent(name)}/`;
|
|
104
|
+
const param = `${parentName ?? "parent"}Id`;
|
|
105
|
+
return `${parentPath.replace(/\/+$/g, "")}/{${param}}/${encodeURIComponent(name)}/`;
|
|
106
|
+
}
|
|
107
|
+
function rebaseDefinition(definition, parentPath, parentName) {
|
|
108
|
+
const path = collectionPath(parentPath, parentName, definition.name);
|
|
109
|
+
return Object.freeze({
|
|
110
|
+
...definition,
|
|
111
|
+
path,
|
|
112
|
+
children: Object.fromEntries(Object.entries(definition.children).map(([name, child]) => [name, rebaseDefinition(child, path, definition.name)]))
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
export { ApiBuilder, CollectionBuilder, CollectionsBuilder, defineCollections };
|
|
117
|
+
|
|
118
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\n\nexport type SchemaOutput<TSchema extends StandardSchemaV1> =\n TSchema extends StandardSchemaV1<any, infer TOutput> ? TOutput : never;\n\nexport interface ApiDefinition<TInput = undefined, TOutput = unknown> {\n input?: StandardSchemaV1;\n output?: StandardSchemaV1;\n path?: string;\n readonly _input?: TInput;\n readonly _output?: TOutput;\n}\n\nexport type ApiDefinitions = Record<string, ApiDefinition<any, any>>;\nexport type CollectionDefinitions = Record<string, CollectionDefinition<any, any, any, any>>;\n\nexport interface CollectionDefinition<\n TRow extends object = Record<string, unknown>,\n TKey extends keyof TRow & string = keyof TRow & string,\n TChildren extends CollectionDefinitions = CollectionDefinitions,\n TApi extends ApiDefinitions = ApiDefinitions,\n> {\n name: string;\n path: string;\n key: TKey;\n schema: StandardSchemaV1;\n children: TChildren;\n api: TApi;\n}\n\nexport interface CollectionSet<TCollections extends CollectionDefinitions = CollectionDefinitions> {\n collections: TCollections;\n}\n\nexport type CollectionPath<TCollections extends CollectionDefinitions> =\n | (keyof TCollections & string)\n | {\n [TName in keyof TCollections &\n string]: `${TName}.${keyof TCollections[TName][\"children\"] & string}`;\n }[keyof TCollections & string];\n\nexport type CollectionAtPath<\n TCollections extends CollectionDefinitions,\n TPath extends string,\n> = TPath extends `${infer THead}.${infer TRest}`\n ? THead extends keyof TCollections & string\n ? CollectionAtPath<TCollections[THead][\"children\"], TRest>\n : never\n : TPath extends keyof TCollections & string\n ? TCollections[TPath]\n : never;\n\nexport function defineCollections(): CollectionsBuilder<{}> {\n return new CollectionsBuilder({});\n}\n\nexport class CollectionsBuilder<TCollections extends CollectionDefinitions> {\n constructor(private readonly collections: TCollections) {}\n\n collection<TName extends string, TBuilder extends CollectionBuilder<any, any, any, any, any>>(\n name: TName,\n configure: (\n collection: CollectionBuilder<TName, Record<string, unknown>, never, {}, {}>,\n ) => TBuilder,\n ): CollectionsBuilder<TCollections & Record<TName, DefinitionFromBuilder<TBuilder>>> {\n const builder = configure(new CollectionBuilder({ name }));\n const definition = builder.toDefinition(\"\", undefined);\n return new CollectionsBuilder({\n ...this.collections,\n [name]: definition,\n } as TCollections & Record<TName, DefinitionFromBuilder<TBuilder>>);\n }\n\n build(): CollectionSet<TCollections> & TCollections {\n return Object.freeze({\n collections: this.collections,\n ...this.collections,\n }) as CollectionSet<TCollections> & TCollections;\n }\n\n rawCollections(): TCollections {\n return this.collections;\n }\n}\n\nexport class CollectionBuilder<\n TName extends string,\n TRow extends object,\n TKey extends string,\n TChildren extends CollectionDefinitions,\n TApi extends ApiDefinitions,\n> {\n constructor(private readonly state: CollectionBuilderState) {}\n\n schema<TNextSchema extends StandardSchemaV1>(\n schema: TNextSchema,\n ): CollectionBuilder<TName, Extract<SchemaOutput<TNextSchema>, object>, never, TChildren, TApi> {\n return new CollectionBuilder({ ...this.state, schema });\n }\n\n key<TKeyNext extends keyof TRow & string>(\n key: TKeyNext,\n ): CollectionBuilder<TName, TRow, TKeyNext, TChildren, TApi> {\n return new CollectionBuilder({ ...this.state, key });\n }\n\n api<TApiName extends string, TBuilder extends ApiBuilder<any, any>>(\n name: TApiName,\n configure: (api: ApiBuilder<undefined, unknown>) => TBuilder,\n ): CollectionBuilder<\n TName,\n TRow,\n TKey,\n TChildren,\n TApi & Record<TApiName, ApiFromBuilder<TBuilder>>\n > {\n const api = configure(new ApiBuilder({}));\n return new CollectionBuilder({\n ...this.state,\n api: {\n ...this.state.api,\n [name]: api.toDefinition(),\n },\n }) as CollectionBuilder<\n TName,\n TRow,\n TKey,\n TChildren,\n TApi & Record<TApiName, ApiFromBuilder<TBuilder>>\n >;\n }\n\n children<TBuilder extends CollectionsBuilder<any>>(\n configure: (children: CollectionsBuilder<{}>) => TBuilder,\n ): CollectionBuilder<TName, TRow, TKey, BuilderCollections<TBuilder>, TApi> {\n return new CollectionBuilder({\n ...this.state,\n children: configure(defineCollections()).rawCollections(),\n }) as CollectionBuilder<TName, TRow, TKey, BuilderCollections<TBuilder>, TApi>;\n }\n\n toDefinition(parentPath: string, parentName: string | undefined): DefinitionFromBuilder<this> {\n if (!this.state.schema) throw new Error(`Collection ${this.state.name} requires a schema`);\n if (!this.state.key) throw new Error(`Collection ${this.state.name} requires a key`);\n\n const path = collectionPath(parentPath, parentName, this.state.name);\n const children = Object.fromEntries(\n Object.entries(this.state.children ?? {}).map(([name, child]) => [\n name,\n rebaseDefinition(child, path, this.state.name),\n ]),\n );\n\n return Object.freeze({\n name: this.state.name,\n path,\n key: this.state.key,\n schema: this.state.schema,\n children,\n api: this.state.api ?? {},\n }) as DefinitionFromBuilder<this>;\n }\n}\n\nexport class ApiBuilder<TInput, TOutput> {\n constructor(private readonly state: ApiBuilderState) {}\n\n input<TSchema extends StandardSchemaV1>(\n schema: TSchema,\n ): ApiBuilder<SchemaOutput<TSchema>, TOutput> {\n return new ApiBuilder({ ...this.state, input: schema });\n }\n\n output<TSchema extends StandardSchemaV1>(\n schema: TSchema,\n ): ApiBuilder<TInput, SchemaOutput<TSchema>> {\n return new ApiBuilder({ ...this.state, output: schema });\n }\n\n path(path: string): ApiBuilder<TInput, TOutput> {\n return new ApiBuilder({ ...this.state, path });\n }\n\n toDefinition(): ApiDefinition<TInput, TOutput> {\n return Object.freeze({ ...this.state }) as ApiDefinition<TInput, TOutput>;\n }\n}\n\ninterface CollectionBuilderState {\n name: string;\n schema?: StandardSchemaV1;\n key?: string;\n children?: CollectionDefinitions;\n api?: ApiDefinitions;\n}\n\ninterface ApiBuilderState {\n input?: StandardSchemaV1;\n output?: StandardSchemaV1;\n path?: string;\n}\n\ntype DefinitionFromBuilder<TBuilder> =\n TBuilder extends CollectionBuilder<any, infer TRow, infer TKey, infer TChildren, infer TApi>\n ? CollectionDefinition<TRow, Extract<TKey, keyof TRow & string>, TChildren, TApi>\n : never;\n\ntype ApiFromBuilder<TBuilder> =\n TBuilder extends ApiBuilder<infer TInput, infer TOutput> ? ApiDefinition<TInput, TOutput> : never;\n\ntype BuilderCollections<TBuilder> =\n TBuilder extends CollectionsBuilder<infer TCollections> ? TCollections : never;\n\nfunction collectionPath(parentPath: string, parentName: string | undefined, name: string): string {\n if (!parentPath) return `/${encodeURIComponent(name)}/`;\n const param = `${parentName ?? \"parent\"}Id`;\n return `${parentPath.replace(/\\/+$/g, \"\")}/{${param}}/${encodeURIComponent(name)}/`;\n}\n\nfunction rebaseDefinition(\n definition: CollectionDefinition,\n parentPath: string,\n parentName: string,\n): CollectionDefinition {\n const path = collectionPath(parentPath, parentName, definition.name);\n return Object.freeze({\n ...definition,\n path,\n children: Object.fromEntries(\n Object.entries(definition.children).map(([name, child]) => [\n name,\n rebaseDefinition(child, path, definition.name),\n ]),\n ),\n });\n}\n"],"mappings":";AAoDA,SAAgB,oBAA4C;CAC1D,OAAO,IAAI,mBAAmB,CAAC,CAAC;AAClC;AAEA,IAAa,qBAAb,MAAa,mBAA+D;CAC7C;CAA7B,YAAY,aAA4C;EAA3B,K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C/C,CAAC,CACH;CACF,CAAC;AACH"}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lsync/definitions",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"homepage": "https://github.com/Myrannas/lsync#readme",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/Myrannas/lsync/issues"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/Myrannas/lsync.git",
|
|
12
|
+
"directory": "packages/definition"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.mts",
|
|
22
|
+
"import": "./dist/index.mjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public",
|
|
27
|
+
"registry": "https://registry.npmjs.org/"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@standard-schema/spec": "^1.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"typescript": "^6.0.3",
|
|
34
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.2",
|
|
35
|
+
"vite-plus": "0.2.2",
|
|
36
|
+
"zod": "^4.4.3"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "vp pack"
|
|
40
|
+
}
|
|
41
|
+
}
|