@magidoc/plugin-starter-variables 2.14.5 → 3.0.0

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/build/index.d.ts CHANGED
@@ -2,8 +2,8 @@ import templates from './variables/templates';
2
2
  import magidoc from './variables/magidoc';
3
3
  import { toVariablesFile, UnsupportedVariablesError } from './env/envFileContent';
4
4
  import type { Variable } from './variables/variable';
5
- import type { Page, ExternalLink } from './variables/templates';
6
- export type { Variable, Page, ExternalLink };
5
+ import type { Page, ExternalLink, AllowedDirective } from './variables/templates';
6
+ export type { Variable, Page, ExternalLink, AllowedDirective };
7
7
  export { templates };
8
8
  export { magidoc };
9
9
  export { toVariablesFile, UnsupportedVariablesError };
@@ -8,6 +8,10 @@ export type ExternalLink = {
8
8
  kind?: string;
9
9
  group?: string;
10
10
  };
11
+ export type AllowedDirective = {
12
+ name: string;
13
+ args: string[];
14
+ };
11
15
  declare const _default: {
12
16
  APP_LOGO: import("./variable").Variable<string>;
13
17
  APP_TITLE: import("./variable").Variable<string>;
@@ -17,5 +21,6 @@ declare const _default: {
17
21
  QUERY_GENERATION_FACTORIES: import("./variable").Variable<Record<string, string | number | boolean | Record<string, unknown> | null | undefined>>;
18
22
  PAGES: import("./variable").Variable<(Page | undefined)[]>;
19
23
  EXTERNAL_LINKS: import("./variable").Variable<(ExternalLink | undefined)[]>;
24
+ DIRECTIVES: import("./variable").Variable<(AllowedDirective | undefined)[]>;
20
25
  };
21
26
  export default _default;
@@ -31,6 +31,12 @@ var templates = {
31
31
  group: zod.string().min(1).optional(),
32
32
  });
33
33
  })),
34
+ DIRECTIVES: createVariable('DIRECTIVES', arrayConverter((zod) => {
35
+ return zod.object({
36
+ name: zod.string().min(1),
37
+ args: zod.array(zod.string()),
38
+ });
39
+ })),
34
40
  };
35
41
 
36
42
  export { templates as default };
@@ -1 +1 @@
1
- {"version":3,"file":"templates.js","sources":["../../../src/variables/templates.ts"],"sourcesContent":[null],"names":[],"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,CAAC;AAC9B,SAAA,CAAC,CAAA;AACJ,KAAC,CAAC,CACH;CACF;;;;"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@magidoc/plugin-starter-variables",
3
3
  "description": "A shared library that contains common Magidoc starter environment variables keys.",
4
4
  "private": false,
5
- "version": "2.14.5",
5
+ "version": "3.0.0",
6
6
  "type": "module",
7
7
  "license": "MIT",
8
8
  "main": "./build/index.js",
@@ -18,12 +18,12 @@
18
18
  "directory": "packages/plugins/starter-variables"
19
19
  },
20
20
  "devDependencies": {
21
- "@rollup/plugin-typescript": "^10.0.0",
22
- "@types/lodash": "^4.14.190",
23
- "@types/node": "^18.11.9",
21
+ "@rollup/plugin-typescript": "^10.0.1",
22
+ "@types/lodash": "^4.14.191",
23
+ "@types/node": "^18.11.10",
24
24
  "@vitest/coverage-c8": "^0.25.3",
25
25
  "lodash": "^4.17.21",
26
- "rollup": "^3.5.0",
26
+ "rollup": "^3.5.1",
27
27
  "tslib": "^2.4.1",
28
28
  "typescript": "^4.9.3",
29
29
  "vite": "^3.2.4",
@@ -32,6 +32,9 @@
32
32
  "optionalDependencies": {
33
33
  "zod": "^3.19.1"
34
34
  },
35
+ "files": [
36
+ "build"
37
+ ],
35
38
  "scripts": {
36
39
  "start": "pnpm build:rollup && node build/index.js",
37
40
  "build": "run-p build:*",
package/.eslintrc.cjs DELETED
@@ -1,9 +0,0 @@
1
- const base = require('../../../.eslintrc.cjs')
2
-
3
- module.exports = {
4
- ...base,
5
- parserOptions: {
6
- ...base.parserOptions,
7
- project: [__dirname + '/tsconfig.json'],
8
- },
9
- }
package/rollup.config.cjs DELETED
@@ -1,14 +0,0 @@
1
- const { defineConfig } = require('rollup')
2
- const typescript = require('@rollup/plugin-typescript')
3
-
4
- module.exports = defineConfig({
5
- input: ['src/index.ts'],
6
- plugins: [typescript()],
7
- external: ['zod', 'lodash'],
8
- output: {
9
- dir: 'build',
10
- format: 'esm',
11
- sourcemap: true,
12
- preserveModules: true,
13
- },
14
- })
@@ -1,60 +0,0 @@
1
- import _ from 'lodash'
2
- import magidoc from '../variables/magidoc'
3
- import type { Variable } from '../variables/variable'
4
-
5
- export function toVariablesFile(
6
- options: Record<string, unknown>,
7
- supportedVariables: ReadonlyArray<Variable<unknown>>,
8
- ): string {
9
- return asVariablesString(buildEnv(options, supportedVariables))
10
- }
11
-
12
- function asVariablesString(env: Record<string, string>) {
13
- return JSON.stringify(env)
14
- }
15
-
16
- function buildEnv(
17
- options: Record<string, unknown>,
18
- supportedVariables: ReadonlyArray<Variable<unknown>>,
19
- ): Record<string, string> {
20
- let newRecord: Record<string, string> = {}
21
- const unsupportedVariables: string[] = []
22
- _.forEach(options, (value, key) => {
23
- const variable = supportedVariables.find((option) => option.name === key)
24
- if (!variable) {
25
- unsupportedVariables.push(key)
26
- return
27
- }
28
-
29
- newRecord = {
30
- ...newRecord,
31
- ...variable.asEnv(value),
32
- }
33
- })
34
-
35
- if (unsupportedVariables.length > 0) {
36
- throw new UnsupportedVariablesError(
37
- `Options [${unsupportedVariables.toString()}] are not supported... Supported option names are [${supportedVariables
38
- .map((value) => value.name)
39
- .join(', ')}]`,
40
- unsupportedVariables,
41
- )
42
- }
43
-
44
- insertDefaultVariables(newRecord)
45
- return newRecord
46
- }
47
-
48
- function insertDefaultVariables(newRecord: Record<string, string>) {
49
- newRecord[magidoc.MAGIDOC_GENERATE.key] = 'true'
50
- }
51
-
52
- export class UnsupportedVariablesError extends Error {
53
- public unsupportedVariables: string[]
54
-
55
- constructor(message: string, unsupportedVariables: string[]) {
56
- super(message)
57
- this.unsupportedVariables = unsupportedVariables
58
- this.name = 'UnsupportedVariableError'
59
- }
60
- }
package/src/index.ts DELETED
@@ -1,13 +0,0 @@
1
- import templates from './variables/templates'
2
- import magidoc from './variables/magidoc'
3
- import {
4
- toVariablesFile,
5
- UnsupportedVariablesError,
6
- } from './env/envFileContent'
7
- import type { Variable } from './variables/variable'
8
- import type { Page, ExternalLink } from './variables/templates'
9
-
10
- export type { Variable, Page, ExternalLink }
11
- export { templates }
12
- export { magidoc }
13
- export { toVariablesFile, UnsupportedVariablesError }
@@ -1,25 +0,0 @@
1
- import type { Converter, ZodTypeProvider } from '../variable'
2
-
3
- export function arrayConverter<V>(
4
- valueType: ZodTypeProvider<V>,
5
- ): Converter<Array<V | undefined>> {
6
- return {
7
- convert: (target) => {
8
- if (Array.isArray(target)) {
9
- return target as Array<V>
10
- }
11
-
12
- if (typeof target === 'string') {
13
- const result = JSON.parse(target) as Record<string, V>
14
- if (!Array.isArray(result)) {
15
- return null
16
- }
17
- return result
18
- }
19
-
20
- return null
21
- },
22
- asString: (value) => JSON.stringify(value),
23
- type: (z) => z.array(valueType(z)).optional(),
24
- }
25
- }
@@ -1,24 +0,0 @@
1
- import type { Converter } from '../variable'
2
-
3
- export function booleanConverter(): Converter<boolean> {
4
- return {
5
- convert: (target: unknown): boolean | null => {
6
- if (target === null) return null
7
- if (target === undefined) return null
8
-
9
- switch (typeof target) {
10
- case 'boolean':
11
- return target
12
- case 'string':
13
- const lowerCase = target.toLowerCase().trim()
14
- return lowerCase === 'true' || lowerCase === 't' || lowerCase === '1'
15
- case 'number':
16
- return target !== 0
17
- default:
18
- return null
19
- }
20
- },
21
- asString: (value) => String(value),
22
- type: (z) => z.boolean().optional(),
23
- }
24
- }
@@ -1,6 +0,0 @@
1
- import { stringConverter } from './string'
2
- import { booleanConverter } from './boolean'
3
- import { recordConverter } from './record'
4
- import { arrayConverter } from './array'
5
-
6
- export { stringConverter, booleanConverter, recordConverter, arrayConverter }
@@ -1,25 +0,0 @@
1
- import type { Converter, ZodTypeProvider } from '../variable'
2
-
3
- export function recordConverter<V>(
4
- valueType: ZodTypeProvider<V>,
5
- ): Converter<Record<string, V | undefined>> {
6
- return {
7
- convert: (target) => {
8
- if (typeof target === 'object') {
9
- return target as Record<string, V>
10
- }
11
-
12
- if (typeof target === 'string') {
13
- const result = JSON.parse(target) as Record<string, V>
14
- if (typeof result !== 'object') {
15
- return null
16
- }
17
- return result
18
- }
19
-
20
- return null
21
- },
22
- asString: (value) => JSON.stringify(value),
23
- type: (z) => z.record(valueType(z).optional()).optional(),
24
- }
25
- }
@@ -1,15 +0,0 @@
1
- import type { Converter } from '../variable'
2
-
3
- export function stringConverter(): Converter<string> {
4
- return {
5
- convert: (target: unknown): string | null => {
6
- if (target === null || target === undefined || target === '') {
7
- return null
8
- }
9
-
10
- return String(target)
11
- },
12
- asString: (value) => String(value),
13
- type: (z) => z.string().optional(),
14
- }
15
- }
@@ -1,9 +0,0 @@
1
- import { booleanConverter } from './converters'
2
- import { createVariable } from './variable'
3
-
4
- export default {
5
- MAGIDOC_GENERATE: createVariable<boolean>(
6
- 'MAGIDOC_GENERATE',
7
- booleanConverter(),
8
- ),
9
- }
@@ -1,66 +0,0 @@
1
- import type { ZodType } from 'zod'
2
- import { arrayConverter, recordConverter, stringConverter } from './converters'
3
- import { createVariable } from './variable'
4
-
5
- export type Page = {
6
- title: string
7
- content: string | Page[]
8
- }
9
-
10
- export type ExternalLink = {
11
- label: string
12
- href: string
13
- kind?: string
14
- group?: string
15
- }
16
-
17
- export default {
18
- APP_LOGO: createVariable<string>('APP_LOGO', stringConverter()),
19
- APP_TITLE: createVariable<string>('APP_TITLE', stringConverter()),
20
- APP_FAVICON: createVariable<string>('APP_FAVICON', stringConverter()),
21
- SITE_ROOT: createVariable<string>('SITE_ROOT', stringConverter()),
22
- SITE_META: createVariable<Record<string, string | undefined>>(
23
- 'SITE_META',
24
- recordConverter((zod) => zod.string()),
25
- ),
26
- QUERY_GENERATION_FACTORIES: createVariable<
27
- Record<
28
- string,
29
- string | boolean | number | Record<string, unknown> | null | undefined
30
- >
31
- >(
32
- 'QUERY_GENERATION_FACTORIES',
33
- recordConverter((zod) =>
34
- zod.union([
35
- zod.string(),
36
- zod.boolean(),
37
- zod.number(),
38
- zod.null(),
39
- zod.record(zod.unknown()),
40
- ]),
41
- ),
42
- ),
43
- PAGES: createVariable<Array<Page | undefined>>(
44
- 'PAGES',
45
- arrayConverter((zod) => {
46
- const type: ZodType<Page> = zod.lazy(() =>
47
- zod.object({
48
- title: zod.string().min(1),
49
- content: zod.union([zod.array(type), zod.string().min(1)]),
50
- }),
51
- )
52
- return type
53
- }),
54
- ),
55
- EXTERNAL_LINKS: createVariable<Array<ExternalLink | undefined>>(
56
- 'EXTERNAL_LINKS',
57
- arrayConverter((zod) => {
58
- return zod.object({
59
- label: zod.string().min(1),
60
- href: zod.string().min(1),
61
- kind: zod.string().min(1).optional(),
62
- group: zod.string().min(1).optional(),
63
- })
64
- }),
65
- ),
66
- }
@@ -1,52 +0,0 @@
1
- import type { ZodType, ZodTypeDef } from 'zod'
2
-
3
- interface MetaEnv {
4
- [key: string]: unknown
5
- }
6
-
7
- export type Variable<T> = {
8
- name: string
9
- key: string
10
- asEnv: (value: T) => Record<string, string>
11
- get: (env: MetaEnv) => T | null
12
- getOrDefault: (env: MetaEnv, def: T) => T
13
- zod: ZodVariable<T>
14
- }
15
-
16
- export type ZodTypeProvider<T> = (
17
- zod: typeof import('zod'),
18
- ) => ZodType<T | undefined, ZodTypeDef, T | undefined>
19
-
20
- export type ZodVariable<T> = {
21
- type: ZodTypeProvider<T>
22
- }
23
-
24
- export type Converter<T> = {
25
- convert: (target: unknown) => T | null
26
- asString: (value: T) => string
27
- type: ZodTypeProvider<T>
28
- }
29
-
30
- export function createVariable<T>(
31
- key: string,
32
- converter: Converter<T>,
33
- ): Variable<T> {
34
- const get = (env: MetaEnv) => converter.convert(env[key])
35
-
36
- return {
37
- name: toCamelCase(key),
38
- key: key,
39
- asEnv: (value: T) => ({ [key]: converter.asString(value) }),
40
- zod: {
41
- type: converter.type,
42
- },
43
- get: get,
44
- getOrDefault: (env, def) => get(env) ?? def,
45
- }
46
- }
47
-
48
- function toCamelCase(key: string): string {
49
- return key.toLowerCase().replace(/([-_][a-z])/gi, ($1) => {
50
- return $1.toUpperCase().replace('-', '').replace('_', '')
51
- })
52
- }
@@ -1,55 +0,0 @@
1
- import { templates, Variable, magidoc, toVariablesFile } from '../../src'
2
- import { UnsupportedVariablesError } from '../../src/env/envFileContent'
3
- import { describe, expect, it } from 'vitest'
4
-
5
- describe('creating a variables file ', () => {
6
- const initialObject = {
7
- appTitle: 'My App',
8
- pages: [
9
- {
10
- title: 'My-page-title',
11
- content:
12
- 'A raw markdown string containing stuff like "double quotes", `code`, and **bold**.',
13
- },
14
- {
15
- title: 'My-page-title-2',
16
- content: [
17
- {
18
- title:
19
- "This is another page, but this one contains 'single quotes', ```much more code```,\n new lines \n\n # And a new header apparently",
20
- },
21
- ],
22
- },
23
- ],
24
- }
25
-
26
- describe('all provided options are supported', () => {
27
- const result = toVariablesFile(initialObject, [
28
- templates.APP_TITLE as Variable<unknown>,
29
- templates.PAGES as Variable<unknown>,
30
- ])
31
-
32
- it('is possible to convert it back to the original object using variables', () => {
33
- const parsed = JSON.parse(result) as Record<string, string>
34
-
35
- expect(magidoc.MAGIDOC_GENERATE.get(parsed)).toBe(true)
36
- expect(templates.APP_TITLE.get(parsed)).toBe(initialObject.appTitle)
37
- expect(templates.PAGES.get(parsed)).toEqual(initialObject.pages)
38
- })
39
- })
40
-
41
- describe('some of the options are unsupported', () => {
42
- it('raises an error', () => {
43
- expect.assertions(2)
44
-
45
- try {
46
- toVariablesFile(initialObject, [])
47
- } catch (error) {
48
- expect(error).toBeInstanceOf(UnsupportedVariablesError)
49
- expect(
50
- (error as UnsupportedVariablesError).unsupportedVariables,
51
- ).toEqual(['appTitle', 'pages'])
52
- }
53
- })
54
- })
55
- })
@@ -1,265 +0,0 @@
1
- import type { ZodType } from 'zod'
2
- import * as variables from '../src/index'
3
- import type { Variable } from '../src/variables/variable'
4
- import z from 'zod'
5
- import type { Page } from '../src/index'
6
- import { describe, expect, it, test } from 'vitest'
7
-
8
- describe('variables', () => {
9
- it('contains the right number of export keys', () => {
10
- expect(Object.keys(variables)).toEqual([
11
- 'templates',
12
- 'magidoc',
13
- 'toVariablesFile',
14
- 'UnsupportedVariablesError',
15
- ])
16
- })
17
-
18
- describe('template variables', () => {
19
- it('contains the right templates variables', () => {
20
- expect(Object.keys(variables.templates)).toEqual([
21
- 'APP_LOGO',
22
- 'APP_TITLE',
23
- 'APP_FAVICON',
24
- 'SITE_ROOT',
25
- 'SITE_META',
26
- 'QUERY_GENERATION_FACTORIES',
27
- 'PAGES',
28
- 'EXTERNAL_LINKS',
29
- ])
30
- })
31
-
32
- test('app title', () => {
33
- testStringVariable(
34
- variables.templates.APP_TITLE,
35
- 'APP_TITLE',
36
- z.string().optional(),
37
- )
38
- })
39
-
40
- test('app logo', () => {
41
- testStringVariable(
42
- variables.templates.APP_LOGO,
43
- 'APP_LOGO',
44
- z.string().optional(),
45
- )
46
- })
47
-
48
- test('app favicon', () => {
49
- testStringVariable(
50
- variables.templates.APP_FAVICON,
51
- 'APP_FAVICON',
52
- z.string().optional(),
53
- )
54
- })
55
-
56
- test('site root', () => {
57
- testStringVariable(
58
- variables.templates.SITE_ROOT,
59
- 'SITE_ROOT',
60
- z.string().optional(),
61
- )
62
- })
63
-
64
- test('site meta', () => {
65
- testRecordVariable(
66
- variables.templates.SITE_META,
67
- 'SITE_META',
68
- z.record(z.string().optional()).optional(),
69
- )
70
- })
71
-
72
- test('query generation factories', () => {
73
- testRecordVariable(
74
- variables.templates.QUERY_GENERATION_FACTORIES,
75
- 'QUERY_GENERATION_FACTORIES',
76
- z
77
- .record(
78
- z
79
- .union([
80
- z.string(),
81
- z.boolean(),
82
- z.number(),
83
- z.null(),
84
- z.record(z.unknown()),
85
- ])
86
- .optional(),
87
- )
88
- .optional(),
89
- )
90
- })
91
-
92
- test('pages', () => {
93
- const pagesType: ZodType<Page> = z.lazy(() =>
94
- z.object({
95
- title: z.string(),
96
- content: z.union([z.array(pagesType), z.string()]),
97
- }),
98
- )
99
-
100
- testArrayVariable(
101
- variables.templates.PAGES,
102
- 'PAGES',
103
- z.array(pagesType).optional(),
104
- )
105
- })
106
-
107
- test('external links', () => {
108
- testArrayVariable(
109
- variables.templates.EXTERNAL_LINKS,
110
- 'EXTERNAL_LINKS',
111
- z
112
- .array(
113
- z.object({
114
- label: z.string().min(1),
115
- href: z.string().min(1),
116
- kind: z.string().min(1).optional(),
117
- group: z.string().min(1).optional(),
118
- }),
119
- )
120
- .optional(),
121
- )
122
- })
123
- })
124
-
125
- describe('magidoc variables', () => {
126
- it('contains the right magidoc variables', () => {
127
- expect(Object.keys(variables.magidoc)).toEqual(['MAGIDOC_GENERATE'])
128
- })
129
-
130
- test('magidoc generate', () => {
131
- testBooleanVariable(
132
- variables.magidoc.MAGIDOC_GENERATE,
133
- 'MAGIDOC_GENERATE',
134
- z.boolean().optional(),
135
- )
136
- })
137
- })
138
- })
139
-
140
- function testStringVariable(
141
- target: Variable<string>,
142
- key: string,
143
- expectedZod: ZodType<any>,
144
- ) {
145
- expect(target.key).toEqual(key)
146
-
147
- expect(target.get({ [key]: 'Potato' })).toBe('Potato')
148
- expect(target.get({ [key]: false })).toBe('false')
149
- expect(target.get({})).toBeNull()
150
-
151
- expect(target.getOrDefault({ [key]: 'Potato' }, 'Default')).toBe('Potato')
152
- expect(target.getOrDefault({ [key]: false }, 'Default')).toBe('false')
153
- expect(target.getOrDefault({}, 'Default')).toBe('Default')
154
-
155
- expect(target.asEnv('Potato')).toEqual({ [key]: 'Potato' })
156
-
157
- ensureZodTypeEqual(target, expectedZod)
158
- }
159
-
160
- function testBooleanVariable(
161
- target: Variable<boolean>,
162
- viteKey: string,
163
- expectedZod: ZodType<any>,
164
- ) {
165
- expect(target.key).toEqual(viteKey)
166
-
167
- expect(target.get({ [viteKey]: true })).toBe(true)
168
- expect(target.get({ [viteKey]: 'true' })).toBe(true)
169
- expect(target.get({ [viteKey]: 't' })).toBe(true)
170
- expect(target.get({ [viteKey]: '1' })).toBe(true)
171
- expect(target.get({ [viteKey]: 12 })).toBe(true)
172
-
173
- expect(target.get({ [viteKey]: false })).toBe(false)
174
- expect(target.get({ [viteKey]: 'false' })).toBe(false)
175
- expect(target.get({ [viteKey]: 'wow' })).toBe(false)
176
- expect(target.get({ [viteKey]: 0 })).toBe(false)
177
-
178
- expect(target.get({ [viteKey]: {} })).toBeNull()
179
- expect(target.get({ [viteKey]: null })).toBeNull()
180
- expect(target.get({ [viteKey]: undefined })).toBeNull()
181
- expect(target.get({})).toBeNull()
182
-
183
- expect(target.getOrDefault({ [viteKey]: false }, true)).toBe(false)
184
- expect(target.getOrDefault({ [viteKey]: {} }, true)).toBe(true)
185
- expect(target.getOrDefault({}, true)).toBe(true)
186
-
187
- expect(target.asEnv(true)).toEqual({ [viteKey]: 'true' })
188
- expect(target.asEnv(false)).toEqual({ [viteKey]: 'false' })
189
-
190
- ensureZodTypeEqual(target, expectedZod)
191
- }
192
-
193
- function testRecordVariable(
194
- target: Variable<Record<string, any>>,
195
- key: string,
196
- expectedZod: ZodType<any>,
197
- ) {
198
- expect(target.key).toEqual(key)
199
-
200
- expect(target.get({ [key]: true })).toBeNull()
201
- expect(target.get({ [key]: 'true' })).toBeNull()
202
- expect(target.get({ [key]: '4234' })).toBeNull()
203
- expect(target.get({ [key]: 4234 })).toBeNull()
204
-
205
- expect(target.get({ [key]: { abc: '123' } })).toEqual({ abc: '123' })
206
- expect(target.get({ [key]: JSON.stringify({ abc: '123' }) })).toEqual({
207
- abc: '123',
208
- })
209
-
210
- expect(target.getOrDefault({ [key]: false }, { abc: 123 })).toEqual({
211
- abc: 123,
212
- })
213
- expect(target.getOrDefault({ [key]: {} }, { abc: 123 })).toEqual({})
214
- expect(target.getOrDefault({}, { abc: 123 })).toEqual({ abc: 123 })
215
-
216
- expect(target.asEnv({ abc: 123 })).toEqual({
217
- [key]: JSON.stringify({ abc: 123 }),
218
- })
219
- expect(target.asEnv({})).toEqual({ [key]: '{}' })
220
-
221
- ensureZodTypeEqual(target, expectedZod)
222
- }
223
-
224
- function testArrayVariable(
225
- target: Variable<Array<any>>,
226
- key: string,
227
- expectedZod: ZodType<any>,
228
- ) {
229
- expect(target.key).toEqual(key)
230
-
231
- expect(target.get({ [key]: true })).toBeNull()
232
- expect(target.get({ [key]: 'true' })).toBeNull()
233
- expect(target.get({ [key]: '4234' })).toBeNull()
234
- expect(target.get({ [key]: 4234 })).toBeNull()
235
- expect(target.get({ [key]: {} })).toBeNull()
236
-
237
- expect(target.get({ [key]: [{ abc: '123' }] })).toEqual([{ abc: '123' }])
238
- expect(target.get({ [key]: JSON.stringify([{ abc: '123' }]) })).toEqual([
239
- { abc: '123' },
240
- ])
241
-
242
- expect(target.getOrDefault({ [key]: false }, [{ abc: 123 }])).toEqual([
243
- {
244
- abc: 123,
245
- },
246
- ])
247
- expect(target.getOrDefault({ [key]: [] }, [{ abc: 123 }])).toEqual([])
248
- expect(target.getOrDefault({}, [{ abc: 123 }])).toEqual([{ abc: 123 }])
249
-
250
- expect(target.asEnv([{ abc: 123 }])).toEqual({
251
- [key]: JSON.stringify([{ abc: 123 }]),
252
- })
253
- expect(target.asEnv([])).toEqual({ [key]: '[]' })
254
-
255
- ensureZodTypeEqual(target, expectedZod)
256
- }
257
-
258
- function ensureZodTypeEqual(target: Variable<any>, expected: ZodType<any>) {
259
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
260
- // @ts-ignore
261
- // this does a good enough compare to make sure they have the same schema
262
- expect(JSON.parse(JSON.stringify(target.zod.type(z)))).toEqual(
263
- JSON.parse(JSON.stringify(expected, null, 2)),
264
- )
265
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "declaration": true,
5
- "emitDeclarationOnly": true,
6
- "outDir": "build"
7
- },
8
- "include": ["src/**/*.ts", "src/**/*.d.ts"]
9
- }
package/tsconfig.json DELETED
@@ -1,27 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "esnext",
4
- "useDefineForClassFields": true,
5
- "importsNotUsedAsValues": "error",
6
- "isolatedModules": true,
7
- "module": "esnext",
8
- "moduleResolution": "node",
9
- "strict": true,
10
- "noImplicitAny": true,
11
- "noImplicitThis": true,
12
- "noImplicitReturns": true,
13
- "sourceMap": true,
14
- "outDir": "build",
15
- "resolveJsonModule": true,
16
- "esModuleInterop": true,
17
- "skipLibCheck": true,
18
- "lib": ["esnext", "dom"]
19
- },
20
- "include": [
21
- "vitest.config.ts",
22
- "src/**/*.ts",
23
- "src/**/*.d.ts",
24
- "tests/**/*.ts"
25
- ],
26
- "exclude": ["build", "node_modules"]
27
- }
package/vitest.config.ts DELETED
@@ -1,13 +0,0 @@
1
- /// <reference types="vitest" />
2
- import { defineConfig } from 'vite'
3
-
4
- export default defineConfig({
5
- test: {
6
- mockReset: true,
7
- coverage: {
8
- all: true,
9
- reporter: ['clover', 'lcov', 'html'],
10
- src: ['./src'],
11
- },
12
- },
13
- })