mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 20:24:47 +00:00
feat: creating the rest of the sdk
This commit is contained in:
@@ -1,33 +1,28 @@
|
||||
import { ManifestVersion } from "../../manifest/ManifestTypes"
|
||||
import { Effects } from "../../types"
|
||||
import { Utils } from "../../util"
|
||||
import { WrapperDataContract } from "../../wrapperData/wrapperDataContract"
|
||||
import { Utils } from "../../util/utils"
|
||||
|
||||
export class Migration<WD, Version extends ManifestVersion> {
|
||||
export class Migration<Store, Version extends ManifestVersion> {
|
||||
constructor(
|
||||
readonly wrapperDataContract: WrapperDataContract<WD>,
|
||||
readonly options: {
|
||||
version: Version
|
||||
up: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
|
||||
up: (opts: { effects: Effects; utils: Utils<Store> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<Store> }) => Promise<void>
|
||||
},
|
||||
) {}
|
||||
static of<WD, Version extends ManifestVersion>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
options: {
|
||||
version: Version
|
||||
up: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
|
||||
},
|
||||
) {
|
||||
return new Migration(wrapperDataContract, options)
|
||||
static of<Store, Version extends ManifestVersion>(options: {
|
||||
version: Version
|
||||
up: (opts: { effects: Effects; utils: Utils<Store> }) => Promise<void>
|
||||
down: (opts: { effects: Effects; utils: Utils<Store> }) => Promise<void>
|
||||
}) {
|
||||
return new Migration(options)
|
||||
}
|
||||
|
||||
async up(opts: { effects: Effects; utils: Utils<WD> }) {
|
||||
async up(opts: { effects: Effects; utils: Utils<Store> }) {
|
||||
this.up(opts)
|
||||
}
|
||||
|
||||
async down(opts: { effects: Effects; utils: Utils<WD> }) {
|
||||
async down(opts: { effects: Effects; utils: Utils<Store> }) {
|
||||
this.down(opts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,39 +4,32 @@ import { SDKManifest } from "../../manifest/ManifestTypes"
|
||||
import { ExpectedExports } from "../../types"
|
||||
import { createUtils } from "../../util"
|
||||
import { once } from "../../util/once"
|
||||
import { WrapperDataContract } from "../../wrapperData/wrapperDataContract"
|
||||
import { Migration } from "./Migration"
|
||||
|
||||
export class Migrations<WD> {
|
||||
export class Migrations<Store> {
|
||||
private constructor(
|
||||
readonly wrapperDataContract: WrapperDataContract<WD>,
|
||||
readonly manifest: SDKManifest,
|
||||
readonly migrations: Array<Migration<WD, any>>,
|
||||
readonly migrations: Array<Migration<Store, any>>,
|
||||
) {}
|
||||
private sortedMigrations = once(() => {
|
||||
const migrationsAsVersions = (
|
||||
this.migrations as Array<Migration<WD, any>>
|
||||
this.migrations as Array<Migration<Store, any>>
|
||||
).map((x) => [EmVer.parse(x.options.version), x] as const)
|
||||
migrationsAsVersions.sort((a, b) => a[0].compareForSort(b[0]))
|
||||
return migrationsAsVersions
|
||||
})
|
||||
private currentVersion = once(() => EmVer.parse(this.manifest.version))
|
||||
static of<WD, Migrations extends Array<Migration<WD, any>>>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
static of<Store, Migrations extends Array<Migration<Store, any>>>(
|
||||
manifest: SDKManifest,
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
) {
|
||||
return new Migrations(
|
||||
wrapperDataContract,
|
||||
manifest,
|
||||
migrations as Array<Migration<WD, any>>,
|
||||
)
|
||||
return new Migrations(manifest, migrations as Array<Migration<Store, any>>)
|
||||
}
|
||||
async init({
|
||||
effects,
|
||||
previousVersion,
|
||||
}: Parameters<ExpectedExports.init>[0]) {
|
||||
const utils = createUtils(this.wrapperDataContract, effects)
|
||||
const utils = createUtils<Store>(effects)
|
||||
if (!!previousVersion) {
|
||||
const previousVersionEmVer = EmVer.parse(previousVersion)
|
||||
for (const [_, migration] of this.sortedMigrations()
|
||||
@@ -50,7 +43,7 @@ export class Migrations<WD> {
|
||||
effects,
|
||||
nextVersion,
|
||||
}: Parameters<ExpectedExports.uninit>[0]) {
|
||||
const utils = createUtils(this.wrapperDataContract, effects)
|
||||
const utils = createUtils<Store>(effects)
|
||||
if (!!nextVersion) {
|
||||
const nextVersionEmVer = EmVer.parse(nextVersion)
|
||||
const reversed = [...this.sortedMigrations()].reverse()
|
||||
@@ -64,14 +57,10 @@ export class Migrations<WD> {
|
||||
}
|
||||
|
||||
export function setupMigrations<
|
||||
WD,
|
||||
Migrations extends Array<Migration<WD, any>>,
|
||||
>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
manifest: SDKManifest,
|
||||
...migrations: EnsureUniqueId<Migrations>
|
||||
) {
|
||||
return Migrations.of(wrapperDataContract, manifest, ...migrations)
|
||||
Store,
|
||||
Migrations extends Array<Migration<Store, any>>,
|
||||
>(manifest: SDKManifest, ...migrations: EnsureUniqueId<Migrations>) {
|
||||
return Migrations.of(manifest, ...migrations)
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
|
||||
@@ -3,10 +3,10 @@ import { Migrations } from "./migrations/setupMigrations"
|
||||
import { Install } from "./setupInstall"
|
||||
import { Uninstall } from "./setupUninstall"
|
||||
|
||||
export function setupInit<WrapperData>(
|
||||
migrations: Migrations<WrapperData>,
|
||||
install: Install<WrapperData>,
|
||||
uninstall: Uninstall<WrapperData>,
|
||||
export function setupInit<Store>(
|
||||
migrations: Migrations<Store>,
|
||||
install: Install<Store>,
|
||||
uninstall: Uninstall<Store>,
|
||||
): {
|
||||
init: ExpectedExports.init
|
||||
uninit: ExpectedExports.uninit
|
||||
|
||||
@@ -1,21 +1,14 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util"
|
||||
import { WrapperDataContract } from "../wrapperData/wrapperDataContract"
|
||||
import { Utils, utils } from "../util/utils"
|
||||
|
||||
export type InstallFn<WD> = (opts: {
|
||||
export type InstallFn<Store> = (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<WD>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void>
|
||||
export class Install<WD> {
|
||||
private constructor(
|
||||
readonly wrapperDataContract: WrapperDataContract<WD>,
|
||||
readonly fn: InstallFn<WD>,
|
||||
) {}
|
||||
static of<WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
fn: InstallFn<WD>,
|
||||
) {
|
||||
return new Install(wrapperDataContract, fn)
|
||||
export class Install<Store> {
|
||||
private constructor(readonly fn: InstallFn<Store>) {}
|
||||
static of<Store>(fn: InstallFn<Store>) {
|
||||
return new Install(fn)
|
||||
}
|
||||
|
||||
async init({
|
||||
@@ -25,14 +18,11 @@ export class Install<WD> {
|
||||
if (!previousVersion)
|
||||
await this.fn({
|
||||
effects,
|
||||
utils: utils(this.wrapperDataContract, effects),
|
||||
utils: utils(effects),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function setupInstall<WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
fn: InstallFn<WD>,
|
||||
) {
|
||||
return Install.of(wrapperDataContract, fn)
|
||||
export function setupInstall<Store>(fn: InstallFn<Store>) {
|
||||
return Install.of(fn)
|
||||
}
|
||||
|
||||
@@ -1,21 +1,14 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { Utils, utils } from "../util"
|
||||
import { WrapperDataContract } from "../wrapperData/wrapperDataContract"
|
||||
import { Utils, utils } from "../util/utils"
|
||||
|
||||
export type UninstallFn<WrapperData> = (opts: {
|
||||
export type UninstallFn<Store> = (opts: {
|
||||
effects: Effects
|
||||
utils: Utils<WrapperData>
|
||||
utils: Utils<Store>
|
||||
}) => Promise<void>
|
||||
export class Uninstall<WD> {
|
||||
private constructor(
|
||||
readonly wrapperDataContract: WrapperDataContract<WD>,
|
||||
readonly fn: UninstallFn<WD>,
|
||||
) {}
|
||||
static of<WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
fn: UninstallFn<WD>,
|
||||
) {
|
||||
return new Uninstall(wrapperDataContract, fn)
|
||||
export class Uninstall<Store> {
|
||||
private constructor(readonly fn: UninstallFn<Store>) {}
|
||||
static of<Store>(fn: UninstallFn<Store>) {
|
||||
return new Uninstall(fn)
|
||||
}
|
||||
|
||||
async uninit({
|
||||
@@ -25,14 +18,11 @@ export class Uninstall<WD> {
|
||||
if (!nextVersion)
|
||||
await this.fn({
|
||||
effects,
|
||||
utils: utils(this.wrapperDataContract, effects),
|
||||
utils: utils(effects),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function setupUninstall<WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
fn: UninstallFn<WD>,
|
||||
) {
|
||||
return Uninstall.of(wrapperDataContract, fn)
|
||||
export function setupUninstall<Store>(fn: UninstallFn<Store>) {
|
||||
return Uninstall.of(fn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user