0.1.6 (#7)
Some checks failed
Publish / publish (push) Failing after 14s
Lint / lint (push) Successful in 12s

Co-authored-by: Julien Valverdé <julien.valverde@mailo.com>
Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
Julien Valverdé
2024-07-29 18:42:05 +02:00
parent 511f43e3f8
commit 7179913d6d
30 changed files with 232 additions and 250 deletions

View File

@@ -23,6 +23,6 @@ jobs:
run: npm run build
- name: Publish
run: |
npm config set @thilawyn:registry https://git.jvalver.de/api/packages/thilawyn/npm/
npm config set @thilawyn:registry https://git.valverde.cloud/api/packages/thilawyn/npm/
npm config set -- //git.jvalver.de/api/packages/thilawyn/npm/:_authToken "${{ vars.NODE_AUTH_TOKEN }}"
npm publish

BIN
bun.lockb

Binary file not shown.

View File

@@ -1,6 +1,6 @@
{
"name": "@thilawyn/thilalib",
"version": "0.1.5",
"version": "0.1.6",
"type": "module",
"files": [
"./dist"
@@ -17,24 +17,34 @@
"default": "./dist/index.cjs"
}
},
"./effect/schema": {
"./Types": {
"import": {
"types": "./dist/effect/schema/index.d.ts",
"default": "./dist/effect/schema/index.js"
"types": "./dist/Types/index.d.ts",
"default": "./dist/Types/index.js"
},
"require": {
"types": "./dist/effect/schema/index.d.cts",
"default": "./dist/effect/schema/index.cjs"
"types": "./dist/Types/index.d.cts",
"default": "./dist/Types/index.cjs"
}
},
"./effect/schema/class": {
"./Schema": {
"import": {
"types": "./dist/effect/schema/class/index.d.ts",
"default": "./dist/effect/schema/class/index.js"
"types": "./dist/Schema/index.d.ts",
"default": "./dist/Schema/index.js"
},
"require": {
"types": "./dist/effect/schema/class/index.d.cts",
"default": "./dist/effect/schema/class/index.cjs"
"types": "./dist/Schema/index.d.cts",
"default": "./dist/Schema/index.cjs"
}
},
"./Schema/MobX": {
"import": {
"types": "./dist/Schema/MobX/index.d.ts",
"default": "./dist/Schema/MobX/index.js"
},
"require": {
"types": "./dist/Schema/MobX/index.d.cts",
"default": "./dist/Schema/MobX/index.cjs"
}
}
},
@@ -46,16 +56,16 @@
"clean:node": "rm -rf node_modules"
},
"dependencies": {
"remeda": "^2.2.2",
"type-fest": "^4.21.0"
"remeda": "^2.6.0",
"type-fest": "^4.23.0"
},
"devDependencies": {
"bun-types": "^1.1.17",
"bun-types": "^1.1.21",
"npm-check-updates": "^16.14.20",
"npm-sort": "^0.0.4",
"tsup": "^8.1.0",
"tsx": "^4.16.0",
"typescript": "^5.5.3"
"tsup": "^8.2.3",
"tsx": "^4.16.2",
"typescript": "^5.5.4"
},
"optionalDependencies": {
"@effect/schema": "*",

17
src/Schema/Class.ts Normal file
View File

@@ -0,0 +1,17 @@
import { Schema } from "@effect/schema"
import type { HasFields } from "./util"
export const Class = Schema.Class as <Self>(identifier: string) =>
<Fields extends Schema.Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>,
annotations?: Schema.Annotations.Schema<Self>,
) => Schema.Class<
Self,
Fields,
Schema.Struct.Encoded<Fields>,
Schema.Struct.Context<Fields>,
Schema.Struct.Constructor<Fields>,
{},
{}
>

13
src/Schema/Jsonifiable.ts Normal file
View File

@@ -0,0 +1,13 @@
import { Schema } from "@effect/schema"
import type { Jsonifiable as TJsonifiable } from "type-fest"
export const Jsonifiable = <
JsonifiableA,
JsonifiableI extends TJsonifiable,
JsonifiableR,
>(
jsonifiable: Schema.Schema<JsonifiableA, JsonifiableI, JsonifiableR>
) =>
<A, R>(schema: Schema.Schema<A, JsonifiableA, R>) =>
jsonifiable.pipe(Schema.compose(schema))

8
src/Schema/Kind.ts Normal file
View File

@@ -0,0 +1,8 @@
import { Schema } from "@effect/schema"
export const Kind = <Kind extends string>(kind: Kind) =>
Schema.withConstructorDefault(
Schema.propertySignature(Schema.Literal(kind)),
() => kind,
)

View File

@@ -0,0 +1,26 @@
import { Schema } from "@effect/schema"
import { makeObservable, observable, type CreateObservableOptions } from "mobx"
import { mapValues } from "remeda"
interface ObservableClassSelf {
new(...args: any[]): Schema.Struct.Type<Schema.Struct.Fields>
readonly fields: { readonly [K in keyof Schema.Struct.Fields]: Schema.Struct.Fields[K] }
}
export const ObservableClass = <Self extends ObservableClassSelf>(
self: Self,
options?: Omit<CreateObservableOptions, "proxy">,
) =>
class Observable extends self {
declare ["constructor"]: typeof Observable
constructor(...args: any[]) {
super(...args)
makeObservable(this,
mapValues(this.constructor.fields, () => observable),
options,
)
}
} as Self

1
src/Schema/MobX/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from "./ObservableClass"

View File

@@ -0,0 +1,54 @@
import { Schema } from "@effect/schema"
import type { Mutable } from "effect/Types"
import type { StaticType } from "../Types"
import type { HasFields } from "./util"
export interface IMutableClass<
Self,
Fields extends Schema.Struct.Fields,
I, R, C,
Inherited,
Proto,
>
extends Omit<
StaticType<Schema.Class<Self, Fields, I, R, C, Inherited, Proto>>,
"extend"
>
{
new(
...args: ConstructorParameters<Schema.Class<Self, Fields, I, R, C, Inherited, Proto>>
): Omit<
InstanceType<Schema.Class<Self, Fields, I, R, C, Inherited, Proto>>,
keyof Fields
> &
Mutable<Schema.Struct.Type<Fields>>
extend<Extended>(identifier: string): <newFields extends Schema.Struct.Fields>(
fields: newFields | HasFields<newFields>,
annotations?: Schema.Annotations.Schema<Extended>,
) => IMutableClass<
Extended,
Fields & newFields,
I & Schema.Struct.Encoded<newFields>,
R | Schema.Struct.Context<newFields>,
C & Schema.Struct.Constructor<newFields>,
Self,
Proto
>
}
export const MutableClass = Schema.Class as <Self>(identifier: string) =>
<Fields extends Schema.Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>,
annotations?: Schema.Annotations.Schema<Self>,
) => IMutableClass<
Self,
Fields,
Schema.Struct.Encoded<Fields>,
Schema.Struct.Context<Fields>,
Schema.Struct.Constructor<Fields>,
{},
{}
>

View File

@@ -0,0 +1,37 @@
import { Schema } from "@effect/schema"
import type { IMutableClass } from "./MutableClass"
import type { HasFields } from "./util"
export interface IMutableTaggedClass<
Self,
Tag,
Fields extends Schema.Struct.Fields,
>
extends IMutableClass<
Self,
Fields,
Schema.Struct.Encoded<Fields>,
Schema.Struct.Context<Fields>,
Schema.Struct.Constructor<Omit<Fields, "_tag">>,
{},
{}
>
{
readonly _tag: Tag
}
export const MutableTaggedClass = Schema.TaggedClass as <Self>(identifier?: string) =>
<
Tag extends string,
Fields extends Schema.Struct.Fields,
>(
tag: Tag,
fieldsOr: Fields | HasFields<Fields>,
annotations?: Schema.Annotations.Schema<Self>,
) => IMutableTaggedClass<
Self,
Tag,
{ readonly _tag: Schema.PropertySignature<":", Tag, never, ":", Tag, true, never> } & Fields
>

8
src/Schema/Tag.ts Normal file
View File

@@ -0,0 +1,8 @@
import { Schema } from "@effect/schema"
export const Tag = <Tag extends string>(tag: Tag) =>
Schema.withConstructorDefault(
Schema.propertySignature(Schema.Literal(tag)),
() => tag,
)

17
src/Schema/TaggedClass.ts Normal file
View File

@@ -0,0 +1,17 @@
import { Schema } from "@effect/schema"
import type { HasFields } from "./util"
export const TaggedClass = Schema.TaggedClass as <Self>(identifier?: string) =>
<
Tag extends string,
Fields extends Schema.Struct.Fields,
>(
tag: Tag,
fieldsOr: Fields | HasFields<Fields>,
annotations?: Schema.Annotations.Schema<Self>,
) => Schema.TaggedClass<
Self,
Tag,
{ readonly _tag: Schema.PropertySignature<":", Tag, never, ":", Tag, true, never> } & Fields
>

8
src/Schema/index.ts Normal file
View File

@@ -0,0 +1,8 @@
export * from "./Class"
export * from "./Jsonifiable"
export * from "./Kind"
export * as MobX from "./MobX"
export * from "./MutableClass"
export * from "./MutableTaggedClass"
export * from "./Tag"
export * from "./TaggedClass"

6
src/Schema/util.ts Normal file
View File

@@ -0,0 +1,6 @@
import { Schema } from "@effect/schema"
export type HasFields<Fields extends Schema.Struct.Fields> =
| { readonly fields: Fields }
| { readonly from: HasFields<Fields> }

1
src/Types/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from "./StaticType"

View File

@@ -1,39 +0,0 @@
import { Schema as S } from "@effect/schema"
import type * as TF from "type-fest"
// export function Jsonifiable<
// A, I, R,
// JsonifiableA extends I, JsonifiableI extends TF.Jsonifiable, JsonifiableR,
// >(
// schema: S.Schema<A, I, R>,
// jsonifiable: S.Schema<JsonifiableA, JsonifiableI, JsonifiableR>,
// ) {
// return jsonifiable.pipe(S.compose(schema))
// }
// export function Jsonifiable<A, I, R>(
// schema: S.Schema<A, I, R>
// ) {
// return <
// JsonifiableA extends I,
// JsonifiableI extends TF.Jsonifiable,
// JsonifiableR,
// >(
// jsonifiable: S.Schema<JsonifiableA, JsonifiableI, JsonifiableR>
// ) =>
// jsonifiable.pipe(S.compose(schema))
// }
export function Jsonifiable<
JsonifiableA,
JsonifiableI extends TF.Jsonifiable,
JsonifiableR,
>(
jsonifiable: S.Schema<JsonifiableA, JsonifiableI, JsonifiableR>
) {
return <A, R>(
schema: S.Schema<A, JsonifiableA, R>
) =>
jsonifiable.pipe(S.compose(schema))
}

View File

@@ -1,9 +0,0 @@
import { Schema as S } from "@effect/schema"
export function Kind<Kind extends string>(kind: Kind) {
return S.withConstructorDefault(
S.propertySignature(S.Literal(kind)),
() => kind,
)
}

View File

@@ -1,9 +0,0 @@
import { Schema as S } from "@effect/schema"
export function Tag<Tag extends string>(tag: Tag) {
return S.withConstructorDefault(
S.propertySignature(S.Literal(tag)),
() => tag,
)
}

View File

@@ -1,17 +0,0 @@
import { Schema as S } from "@effect/schema"
import type { HasFields } from "./util"
export const Class = S.Class as <Self>(identifier: string) =>
<Fields extends S.Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>,
annotations?: S.Annotations.Schema<Self>,
) => S.Class<
Self,
Fields,
S.Struct.Encoded<Fields>,
S.Struct.Context<Fields>,
S.Struct.Constructor<Fields>,
{},
{}
>

View File

@@ -1,27 +0,0 @@
import { Schema as S } from "@effect/schema"
import { makeObservable, observable, type CreateObservableOptions } from "mobx"
import { mapValues } from "remeda"
interface MobXObservableA {
new(...args: any[]): S.Struct.Type<S.Struct.Fields>
readonly fields: { readonly [K in keyof S.Struct.Fields]: S.Struct.Fields[K] }
}
export function MobXObservable<A extends MobXObservableA>(
self: A,
options?: Omit<CreateObservableOptions, "proxy">,
) {
return class MobXObservable extends self {
declare ["constructor"]: typeof MobXObservable
constructor(...args: any[]) {
super(...args)
makeObservable(this,
mapValues(this.constructor.fields, () => observable),
options,
)
}
} as A
}

View File

@@ -1,54 +0,0 @@
import { Schema as S } from "@effect/schema"
import type { Mutable } from "effect/Types"
import type { StaticType } from "../../../StaticType"
import type { HasFields } from "./util"
export interface IMutableClass<
Self,
Fields extends S.Struct.Fields,
I, R, C,
Inherited,
Proto,
>
extends Omit<
StaticType<S.Class<Self, Fields, I, R, C, Inherited, Proto>>,
"extend"
>
{
new(
...args: ConstructorParameters<S.Class<Self, Fields, I, R, C, Inherited, Proto>>
): Omit<
InstanceType<S.Class<Self, Fields, I, R, C, Inherited, Proto>>,
keyof Fields
> &
Mutable<S.Struct.Type<Fields>>
extend<Extended>(identifier: string): <newFields extends S.Struct.Fields>(
fields: newFields | HasFields<newFields>,
annotations?: S.Annotations.Schema<Extended>,
) => IMutableClass<
Extended,
Fields & newFields,
I & S.Struct.Encoded<newFields>,
R | S.Struct.Context<newFields>,
C & S.Struct.Constructor<newFields>,
Self,
Proto
>
}
export const MutableClass = S.Class as <Self>(identifier: string) =>
<Fields extends S.Struct.Fields>(
fieldsOr: Fields | HasFields<Fields>,
annotations?: S.Annotations.Schema<Self>,
) => IMutableClass<
Self,
Fields,
S.Struct.Encoded<Fields>,
S.Struct.Context<Fields>,
S.Struct.Constructor<Fields>,
{},
{}
>

View File

@@ -1,37 +0,0 @@
import { Schema as S } from "@effect/schema"
import type { IMutableClass } from "./MutableClass"
import type { HasFields } from "./util"
export interface IMutableTaggedClass<
Self,
Tag,
Fields extends S.Struct.Fields,
>
extends IMutableClass<
Self,
Fields,
S.Struct.Encoded<Fields>,
S.Struct.Context<Fields>,
S.Struct.Constructor<Omit<Fields, "_tag">>,
{},
{}
>
{
readonly _tag: Tag
}
export const MutableTaggedClass = S.TaggedClass as <Self>(identifier?: string) =>
<
Tag extends string,
Fields extends S.Struct.Fields,
>(
tag: Tag,
fieldsOr: Fields | HasFields<Fields>,
annotations?: S.Annotations.Schema<Self>,
) => IMutableTaggedClass<
Self,
Tag,
{ readonly _tag: S.PropertySignature<":", Tag, never, ":", Tag, true, never> } & Fields
>

View File

@@ -1,17 +0,0 @@
import { Schema as S } from "@effect/schema"
import type { HasFields } from "./util"
export const TaggedClass = S.TaggedClass as <Self>(identifier?: string) =>
<
Tag extends string,
Fields extends S.Struct.Fields,
>(
tag: Tag,
fieldsOr: Fields | HasFields<Fields>,
annotations?: S.Annotations.Schema<Self>,
) => S.TaggedClass<
Self,
Tag,
{ readonly _tag: S.PropertySignature<":", Tag, never, ":", Tag, true, never> } & Fields
>

View File

@@ -1,7 +0,0 @@
export { Class } from "./Class"
export { MobXObservable } from "./MobXObservable"
export { MutableClass } from "./MutableClass"
export type { IMutableClass } from "./MutableClass"
export { MutableTaggedClass } from "./MutableTaggedClass"
export type { IMutableTaggedClass } from "./MutableTaggedClass"
export { TaggedClass } from "./TaggedClass"

View File

@@ -1,7 +0,0 @@
import { Schema as S } from "@effect/schema"
export type HasFields<Fields extends S.Struct.Fields> = (
| { readonly fields: Fields }
| { readonly from: HasFields<Fields> }
)

View File

@@ -1,3 +0,0 @@
export { Jsonifiable } from "./Jsonifiable"
export { Kind } from "./Kind"
export { Tag } from "./Tag"

View File

@@ -1 +1,2 @@
export type { StaticType } from "./StaticType"
export * as Schema from "./Schema"
export * as Types from "./Types"

View File

@@ -1,14 +1,14 @@
import { Schema as S } from "@effect/schema"
import { reaction, runInAction } from "mobx"
import { Jsonifiable } from "./Jsonifiable"
import { MobXObservable, MutableTaggedClass } from "./class"
import { Jsonifiable, MutableTaggedClass } from "./Schema"
import { ObservableClass } from "./Schema/MobX"
class User extends MutableTaggedClass<User>()("User", {
id: S.BigIntFromSelf,
role: S.Union(S.Literal("BasicUser"), S.Literal("Admin")),
}).pipe(
MobXObservable
ObservableClass
) {}
const JsonifiableUser = User.pipe(

View File

@@ -4,8 +4,9 @@ import { defineConfig } from "tsup"
export default defineConfig({
entry: [
"./src/index.ts",
"./src/effect/schema/index.ts",
"./src/effect/schema/class/index.ts",
"./src/Types/index.ts",
"./src/Schema/index.ts",
"./src/Schema/MobX/index.ts",
],
format: ["esm", "cjs"],
skipNodeModulesBundle: true,