Skip to content

Commit

Permalink
Refactor/remove poppinss utils (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored Mar 14, 2023
1 parent 4bed5b5 commit 648b5f0
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 68 deletions.
31 changes: 15 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,30 @@
"access": "public"
},
"dependencies": {
"@poppinss/utils": "^5.0.0",
"validator": "^13.7.0"
"validator": "^13.9.0"
},
"devDependencies": {
"@adonisjs/mrm-preset": "^5.0.3",
"@adonisjs/require-ts": "^2.0.13",
"@japa/assert": "^1.3.5",
"@japa/expect-type": "^1.0.2",
"@japa/run-failed-tests": "^1.0.8",
"@japa/runner": "^2.2.2",
"@japa/spec-reporter": "^1.3.2",
"@types/node": "^18.11.18",
"@types/validator": "^13.7.10",
"commitizen": "^4.2.6",
"@japa/assert": "^1.4.1",
"@japa/expect-type": "^1.0.3",
"@japa/run-failed-tests": "^1.1.1",
"@japa/runner": "^2.5.1",
"@japa/spec-reporter": "^1.3.3",
"@types/node": "^18.15.2",
"@types/validator": "^13.7.14",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"del-cli": "^5.0.0",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.7.0",
"eslint-plugin-adonis": "^2.1.1",
"eslint-plugin-prettier": "^4.2.1",
"github-label-sync": "^2.2.0",
"husky": "^8.0.2",
"github-label-sync": "^2.3.1",
"husky": "^8.0.3",
"np": "^7.6.3",
"prettier": "^2.8.1",
"typescript": "^4.9.4"
"prettier": "^2.8.4",
"typescript": "^4.9.5"
},
"repository": {
"type": "git",
Expand Down
7 changes: 2 additions & 5 deletions src/schema/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

import { Exception } from '@poppinss/utils'
import { SchemaFnOptions } from '../contracts'
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers'

Expand All @@ -23,11 +22,9 @@ function castToBoolean(key: string, value: string, message?: string): boolean {
return false
}

throw new Exception(
throw new Error(
message ||
`Value for environment variable "${key}" must be a boolean, instead received "${value}"`,
500,
'E_INVALID_ENV_VALUE'
`Value for environment variable "${key}" must be a boolean, instead received "${value}"`
)
}

Expand Down
8 changes: 1 addition & 7 deletions src/schema/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* file that was distributed with this source code.
*/

import { Exception } from '@poppinss/utils'

/**
* Following values are considered as "true"
*/
Expand All @@ -28,10 +26,6 @@ export function ensureValue(
message?: string
): asserts value is string {
if (!value) {
throw new Exception(
message || `Missing environment variable "${key}"`,
500,
'E_MISSING_ENV_VALUE'
)
throw new Error(message || `Missing environment variable "${key}"`)
}
}
7 changes: 2 additions & 5 deletions src/schema/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

import { Exception } from '@poppinss/utils'
import { SchemaFnOptions } from '../contracts'
import { ensureValue } from './helpers'

Expand All @@ -17,11 +16,9 @@ import { ensureValue } from './helpers'
export function castToNumber(key: string, value: string, message?: string): number {
const castedValue = Number(value)
if (isNaN(castedValue)) {
throw new Exception(
throw new Error(
message ||
`Value for environment variable "${key}" must be numeric, instead received "${value}"`,
500,
'E_INVALID_ENV_VALUE'
`Value for environment variable "${key}" must be numeric, instead received "${value}"`
)
}

Expand Down
7 changes: 2 additions & 5 deletions src/schema/oneOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

import { Exception } from '@poppinss/utils'
import { SchemaFnOptions } from '../contracts'
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers'

Expand Down Expand Up @@ -43,13 +42,11 @@ function ensureOneOf(choices: readonly any[], key: string, value: any, message?:
/**
* Otherwise raise exception
*/
throw new Exception(
throw new Error(
message ||
`Value for environment variable "${key}" must be one of "${choices.join(
','
)}", instead received "${value}"`,
500,
'E_INVALID_ENV_VALUE'
)}", instead received "${value}"`
)
}

Expand Down
19 changes: 6 additions & 13 deletions src/schema/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

import { Exception } from '@poppinss/utils'
import { StringFnOptions, StringFnUrlOptions } from '../contracts'
import { ensureValue } from './helpers'

Expand All @@ -24,11 +23,9 @@ const formats: {
} = {
email: (key: string, value: string, options: StringFnOptions) => {
if (!require('validator/lib/isEmail')(value)) {
throw new Exception(
throw new Error(
options.message ||
`Value for environment variable "${key}" must be a valid email, instead received "${value}"`,
500,
'E_INVALID_ENV_VALUE'
`Value for environment variable "${key}" must be a valid email, instead received "${value}"`
)
}
},
Expand All @@ -37,22 +34,18 @@ const formats: {
!require('validator/lib/isFQDN')(value, { require_tld: false }) &&
!require('validator/lib/isIP')(value)
) {
throw new Exception(
throw new Error(
options.message ||
`Value for environment variable "${key}" must be a valid (domain or ip), instead received "${value}"`,
500,
'E_INVALID_ENV_VALUE'
`Value for environment variable "${key}" must be a valid (domain or ip), instead received "${value}"`
)
}
},
url: (key: string, value: string, options: StringFnUrlOptions) => {
const { tld = true, protocol = true } = options
if (!require('validator/lib/isURL')(value, { require_tld: tld, require_protocol: protocol })) {
throw new Exception(
throw new Error(
options.message ||
`Value for environment variable "${key}" must be a valid URL, instead received "${value}"`,
500,
'E_INVALID_ENV_VALUE'
`Value for environment variable "${key}" must be a valid URL, instead received "${value}"`
)
}
},
Expand Down
28 changes: 11 additions & 17 deletions tests/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import { schema } from '../src/schema'
test.group('schema | number', () => {
test('raise error when value is missing', ({ assert }) => {
const fn = () => schema.number()('PORT')
assert.throws(fn, 'E_MISSING_ENV_VALUE: Missing environment variable "PORT"')
assert.throws(fn, 'Missing environment variable "PORT"')
})

test('raise error when value is not a valid number', ({ assert }) => {
const fn = () => schema.number()('PORT', 'foo')
assert.throws(fn, 'E_INVALID_ENV_VALUE: Value for environment variable "PORT" must be numeric')
assert.throws(fn, 'Value for environment variable "PORT" must be numeric')
})

test('raise error when value is an empty string', ({ assert }) => {
const fn = () => schema.number()('PORT', '')
assert.throws(fn, 'E_MISSING_ENV_VALUE: Missing environment variable "PORT"')
assert.throws(fn, 'Missing environment variable "PORT"')
})

test('cast string representation of number to a number', ({ assert, expectTypeOf }) => {
Expand Down Expand Up @@ -54,7 +54,7 @@ test.group('schema | number.optional', () => {

test('raise error when value is not a valid number', ({ assert }) => {
const fn = () => schema.number.optional()('PORT', 'foo')
assert.throws(fn, 'E_INVALID_ENV_VALUE: Value for environment variable "PORT" must be numeric')
assert.throws(fn, 'Value for environment variable "PORT" must be numeric')
})

test('return undefined when value is an empty string', ({ assert, expectTypeOf }) => {
Expand Down Expand Up @@ -85,7 +85,7 @@ test.group('schema | number.optional', () => {
test.group('schema | string', () => {
test('raise error when value is missing', ({ assert }) => {
const fn = () => schema.string()('PORT')
assert.throws(fn, 'E_MISSING_ENV_VALUE: Missing environment variable "PORT"')
assert.throws(fn, 'Missing environment variable "PORT"')
})

test('return string value as it is', ({ assert, expectTypeOf }) => {
Expand Down Expand Up @@ -159,20 +159,17 @@ test.group('schema | string.optional', () => {
test.group('schema | boolean', () => {
test('raise error when value is missing', ({ assert }) => {
const fn = () => schema.boolean()('CACHE_VIEWS')
assert.throws(fn, 'E_MISSING_ENV_VALUE: Missing environment variable "CACHE_VIEWS"')
assert.throws(fn, 'Missing environment variable "CACHE_VIEWS"')
})

test('raise error when value is not a valid boolean', ({ assert }) => {
const fn = () => schema.boolean()('CACHE_VIEWS', 'foo')
assert.throws(
fn,
'E_INVALID_ENV_VALUE: Value for environment variable "CACHE_VIEWS" must be a boolean'
)
assert.throws(fn, 'Value for environment variable "CACHE_VIEWS" must be a boolean')
})

test('raise error when value is an empty string', ({ assert }) => {
const fn = () => schema.boolean()('CACHE_VIEWS', '')
assert.throws(fn, 'E_MISSING_ENV_VALUE: Missing environment variable "CACHE_VIEWS"')
assert.throws(fn, 'Missing environment variable "CACHE_VIEWS"')
})

test('cast "true" to a boolean', ({ assert, expectTypeOf }) => {
Expand Down Expand Up @@ -209,10 +206,7 @@ test.group('schema | boolean.optional', () => {

test('raise error when value is not a valid boolean', ({ assert }) => {
const fn = () => schema.boolean.optional()('CACHE_VIEWS', 'foo')
assert.throws(
fn,
'E_INVALID_ENV_VALUE: Value for environment variable "CACHE_VIEWS" must be a boolean'
)
assert.throws(fn, 'Value for environment variable "CACHE_VIEWS" must be a boolean')
})

test('return undefined when value is an empty string', ({ assert, expectTypeOf }) => {
Expand Down Expand Up @@ -249,7 +243,7 @@ test.group('schema | boolean.optional', () => {
test.group('schema | enum', () => {
test('raise error when value is missing', ({ assert }) => {
const fn = () => schema.enum(['api', 'web'])('AUTH_GUARD')
assert.throws(fn, 'E_MISSING_ENV_VALUE: Missing environment variable "AUTH_GUARD"')
assert.throws(fn, 'Missing environment variable "AUTH_GUARD"')
})

test('raise error when value is not one of the defined options', ({ assert }) => {
Expand All @@ -259,7 +253,7 @@ test.group('schema | enum', () => {

test('raise error when value is an empty string', ({ assert }) => {
const fn = () => schema.enum(['api', 'web'])('AUTH_GUARD')
assert.throws(fn, 'E_MISSING_ENV_VALUE: Missing environment variable "AUTH_GUARD"')
assert.throws(fn, 'Missing environment variable "AUTH_GUARD"')
})

test('return value when it is in one of the defined choices', ({ assert, expectTypeOf }) => {
Expand Down

0 comments on commit 648b5f0

Please sign in to comment.