Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,6 @@ function applySchema<ParsedInput, ParsedContext>(
}
}

/**
* @deprecated use `applySchema` instead
*/
function withSchema<ParsedInput, ParsedContext>(
inputSchema?: StandardSchema<unknown, ParsedInput>,
contextSchema?: StandardSchema<unknown, ParsedContext>,
) {
return applySchema(inputSchema, contextSchema)
}

const alwaysUnknownSchema: StandardSchema<unknown, unknown> = {
'~standard': {
vendor: 'composable-functions',
Expand All @@ -164,4 +154,4 @@ const alwaysUnknownSchema: StandardSchema<unknown, unknown> = {
},
}

export { applySchema, composable, failure, fromSuccess, success, withSchema }
export { applySchema, composable, failure, fromSuccess, success }
13 changes: 0 additions & 13 deletions src/context/context.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/context/environment.ts

This file was deleted.

40 changes: 2 additions & 38 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ class InputError extends Error {
}
}

/**
* @deprecated Use `ContextError` instead
* A custom error class for context errors.
*
* @example
* const fn = () => {
* throw new EnvironmentError('Invalid environment', 'user.name')
* }
*/
class EnvironmentError extends Error {
/**
* Path of context attribute that originated the error.
*/
path: string[]

constructor(message: string, path: string[] = []) {
super(message)
this.name = 'EnvironmentError'
this.path = path
}
}

/**
* A custom error class for context errors.
*
Expand Down Expand Up @@ -87,25 +65,11 @@ function isInputError(e: { name: string; message: string }): boolean {
return e.name === 'InputError'
}

/**
* @deprecated Use `isContextError` instead
* A function to check if an `Error` or a `SerializableError` is a ContextError
*/
const isEnvironmentError = isContextError

/**
* A function to check if an `Error` or a `SerializableError` is a ContextError
*/
function isContextError(e: { name: string; message: string }): boolean {
return e.name === 'EnvironmentError' || e.name === 'ContextError'
return e.name === 'ContextError'
}

export {
ContextError,
EnvironmentError,
ErrorList,
InputError,
isContextError,
isEnvironmentError,
isInputError,
}
export { ContextError, ErrorList, InputError, isContextError, isInputError }
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export {
failure,
fromSuccess,
success,
withSchema,
} from './constructors.ts'
export {
all,
Expand Down Expand Up @@ -33,11 +32,9 @@ export type {
export { serialize, serializeError } from './serializer.ts'
export {
ContextError,
EnvironmentError,
ErrorList,
InputError,
isContextError,
isEnvironmentError,
isInputError,
} from './errors.ts'
export type {
Expand All @@ -63,6 +60,4 @@ export type {
} from './types.ts'

// FUNCTIONS WITH CONTEXT
export { environment } from './context/environment.ts'
export { context } from './context/context.ts'
export * as withContext from './context/index.ts'
55 changes: 6 additions & 49 deletions src/tests/constructors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
fromSuccess,
InputError,
success,
withSchema,
} from '../index.ts'
import { applySchema } from '../index.ts'
import type { Internal } from '../internal/types.ts'
Expand Down Expand Up @@ -172,17 +171,17 @@ describe('fromSuccess', () => {
})
})

describe('withSchema', () => {
describe('applySchema', () => {
describe('when it has no input', () => {
it('uses zod parser to create parse the input and call the schema function', async () => {
const handler = withSchema()(() => 'no input!')
const handler = applySchema()(() => 'no input!')
type _R = Expect<Equal<typeof handler, ComposableWithSchema<string>>>

assertEquals(await handler(), success('no input!'))
})

it('defaults non-declared input to unknown', async () => {
const handler = withSchema()((args) => args)
const handler = applySchema()((args) => args)
type _R = Expect<Equal<typeof handler, ComposableWithSchema<unknown>>>

assertEquals(await handler('some input'), {
Expand All @@ -194,25 +193,16 @@ describe('withSchema', () => {
})

describe('when it has no context', () => {
it('uses zod parser to create parse the input and call the schema function', async () => {
const parser = z.object({ id: z.preprocess(Number, z.number()) })

const handler = withSchema(parser)(({ id }) => id)
type _R = Expect<Equal<typeof handler, ComposableWithSchema<number>>>

assertEquals(await handler({ id: '1' }), success(1))
})

it('fails gracefully if gets something other than empty record', async () => {
const handler = withSchema()(() => 'no input!')
const handler = applySchema()(() => 'no input!')
type _R = Expect<Equal<typeof handler, ComposableWithSchema<string>>>

assertEquals(await handler(undefined, ''), success('no input!'))
})

it('returns error when parsing fails', async () => {
const parser = z.object({ id: z.preprocess(Number, z.number()) })
const handler = withSchema(parser)(({ id }) => id)
const handler = applySchema(parser)(({ id }) => id)
type _R = Expect<Equal<typeof handler, ComposableWithSchema<number>>>

assertEquals(
Expand All @@ -223,45 +213,12 @@ describe('withSchema', () => {
})

it('accepts a composable', async () => {
const handler = withSchema()(composable(() => 'no input!'))
const handler = applySchema()(composable(() => 'no input!'))
type _R = Expect<Equal<typeof handler, ComposableWithSchema<string>>>

assertEquals(await handler(), success('no input!'))
})

it('defaults non-declared input to unknown', async () => {
const handler = withSchema()((args) => args)
type _R = Expect<Equal<typeof handler, ComposableWithSchema<unknown>>>

assertEquals(await handler('some input'), {
success: true,
data: 'some input',
errors: [],
})
})
Comment on lines -232 to -241
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed only the tests that have a matching one under applySchema tests


it('uses zod parsers to parse the input and context and call the schema function', async () => {
const parser = z.object({ id: z.preprocess(Number, z.number()) })
const ctxParser = z.object({ uid: z.preprocess(Number, z.number()) })

const handler = withSchema(
parser,
ctxParser,
)(({ id }, { uid }) => [id, uid] as const)
type _R = Expect<
Equal<
typeof handler,
Composable<
(input?: unknown, context?: unknown) => readonly [number, number]
>
>
>

assertEquals(await handler({ id: '1' }, { uid: '2' }), success([1, 2]))
})
})

describe('applySchema', () => {
it('uses zod parsers to parse the input and context turning it into a schema function', async () => {
const inputSchema = z.object({ id: z.preprocess(Number, z.number()) })
const ctxSchema = z.object({ uid: z.preprocess(Number, z.number()) })
Expand Down
11 changes: 0 additions & 11 deletions src/tests/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { assertEquals, describe, it } from './prelude.ts'
import {
ContextError,
EnvironmentError,
InputError,
isContextError,
isEnvironmentError,
isInputError,
} from '../index.ts'

Expand All @@ -13,7 +11,6 @@ describe('isInputError', () => {
assertEquals(isInputError(new Error('No')), false)
assertEquals(isInputError(new InputError('Yes')), true)
assertEquals(isInputError(new ContextError('No')), false)
assertEquals(isInputError(new EnvironmentError('No')), false)
})
})

Expand All @@ -22,13 +19,5 @@ describe('isContextError', () => {
assertEquals(isContextError(new Error('No')), false)
assertEquals(isContextError(new InputError('No')), false)
assertEquals(isContextError(new ContextError('Yes')), true)
assertEquals(isContextError(new EnvironmentError('Yes')), true)
})

it('checks if an error is instance of legacy EnvironmentError', () => {
assertEquals(isEnvironmentError(new Error('No')), false)
assertEquals(isEnvironmentError(new InputError('No')), false)
assertEquals(isEnvironmentError(new ContextError('Yes')), true)
assertEquals(isEnvironmentError(new EnvironmentError('Yes')), true)
})
})