Skip to content

Commit

Permalink
chore: use the latest eslint and @hono/eslint-config (#904)
Browse files Browse the repository at this point in the history
* chore: use the latest eslint and `@hono/eslint-config`

* update codes
  • Loading branch information
yusukebe authored Dec 25, 2024
1 parent 755d5cb commit 7a401b0
Show file tree
Hide file tree
Showing 56 changed files with 671 additions and 424 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

3 changes: 0 additions & 3 deletions .eslintrc.js

This file was deleted.

8 changes: 8 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import baseConfig from './packages/eslint-config/index.js'

export default [
...baseConfig,
{
ignores: ['**/dist/*'],
},
]
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^8.7.0",
"@typescript-eslint/parser": "^8.7.0",
"eslint": "^8.57.0",
"eslint-plugin-import-x": "^4.1.1",
"eslint-plugin-n": "^17.10.2",
"eslint": "^9.17.0",
"jest": "^29.5.0",
"jest-environment-miniflare": "^2.14.1",
"npm-run-all2": "^6.2.2",
Expand Down
35 changes: 18 additions & 17 deletions packages/ajv-validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Context, Env, MiddlewareHandler, ValidationTargets } from 'hono';
import { validator } from 'hono/validator';
import { Ajv, type JSONSchemaType, type ErrorObject } from 'ajv';
import { Ajv } from 'ajv'
import type { JSONSchemaType, ErrorObject } from 'ajv'
import type { Context, Env, MiddlewareHandler, ValidationTargets } from 'hono'
import { validator } from 'hono/validator'

type Hook<T, E extends Env, P extends string> = (
result: { success: true; data: T } | { success: false; errors: ErrorObject[] },
c: Context<E, P>
) => Response | Promise<Response> | void;
) => Response | Promise<Response> | void

/**
* Hono middleware that validates incoming data via an Ajv JSON schema.
Expand Down Expand Up @@ -75,32 +76,32 @@ export function ajvValidator<
E,
P,
{
in: { [K in Target]: T };
out: { [K in Target]: T };
in: { [K in Target]: T }
out: { [K in Target]: T }
}
> {
const ajv = new Ajv();
const validate = ajv.compile(schema);
const ajv = new Ajv()
const validate = ajv.compile(schema)

return validator(target, (data, c) => {
const valid = validate(data);
const valid = validate(data)
if (valid) {
if (hook) {
const hookResult = hook({ success: true, data: data as T }, c);
const hookResult = hook({ success: true, data: data as T }, c)
if (hookResult instanceof Response || hookResult instanceof Promise) {
return hookResult;
return hookResult
}
}
return data as T;
return data as T
}

const errors = validate.errors || [];
const errors = validate.errors || []
if (hook) {
const hookResult = hook({ success: false, errors }, c);
const hookResult = hook({ success: false, errors }, c)
if (hookResult instanceof Response || hookResult instanceof Promise) {
return hookResult;
return hookResult
}
}
return c.json({ success: false, errors }, 400);
});
return c.json({ success: false, errors }, 400)
})
}
140 changes: 70 additions & 70 deletions packages/ajv-validator/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Hono } from 'hono';
import type { Equal, Expect } from 'hono/utils/types';
import { ajvValidator } from '../src';
import { JSONSchemaType, type ErrorObject } from 'ajv';
import type { JSONSchemaType, type ErrorObject } from 'ajv'
import { Hono } from 'hono'
import type { Equal, Expect } from 'hono/utils/types'
import { ajvValidator } from '../src'

type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never;
type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never

describe('Basic', () => {
const app = new Hono();
const app = new Hono()

const schema: JSONSchemaType<{ name: string; age: number }> = {
type: 'object',
Expand All @@ -16,35 +16,35 @@ describe('Basic', () => {
},
required: ['name', 'age'],
additionalProperties: false,
};
}

const route = app.post('/author', ajvValidator('json', schema), (c) => {
const data = c.req.valid('json');
const data = c.req.valid('json')
return c.json({
success: true,
message: `${data.name} is ${data.age}`,
});
});
})
})

type Actual = ExtractSchema<typeof route>;
type Actual = ExtractSchema<typeof route>
type Expected = {
'/author': {
$post: {
input: {
json: {
name: string;
age: number;
};
};
name: string
age: number
}
}
output: {
success: boolean;
message: string;
};
};
};
};
success: boolean
message: string
}
}
}
}

type verify = Expect<Equal<Expected, Actual>>;
type verify = Expect<Equal<Expected, Actual>>

it('Should return 200 response', async () => {
const req = new Request('http://localhost/author', {
Expand All @@ -56,15 +56,15 @@ describe('Basic', () => {
headers: {
'Content-Type': 'application/json',
},
});
const res = await app.request(req);
expect(res).not.toBeNull();
expect(res.status).toBe(200);
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
success: true,
message: 'Superman is 20',
});
});
})
})

it('Should return 400 response', async () => {
const req = new Request('http://localhost/author', {
Expand All @@ -76,17 +76,17 @@ describe('Basic', () => {
headers: {
'Content-Type': 'application/json',
},
});
const res = await app.request(req);
expect(res).not.toBeNull();
expect(res.status).toBe(400);
const data = (await res.json()) as { success: boolean };
expect(data.success).toBe(false);
});
});
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(400)
const data = (await res.json()) as { success: boolean }
expect(data.success).toBe(false)
})
})

describe('With Hook', () => {
const app = new Hono();
const app = new Hono()

const schema: JSONSchemaType<{ id: number; title: string }> = {
type: 'object',
Expand All @@ -96,39 +96,39 @@ describe('With Hook', () => {
},
required: ['id', 'title'],
additionalProperties: false,
};
}

app
.post(
'/post',
ajvValidator('json', schema, (result, c) => {
if (!result.success) {
return c.text('Invalid!', 400);
return c.text('Invalid!', 400)
}
const data = result.data;
return c.text(`${data.id} is valid!`);
const data = result.data
return c.text(`${data.id} is valid!`)
}),
(c) => {
const data = c.req.valid('json');
const data = c.req.valid('json')
return c.json({
success: true,
message: `${data.id} is ${data.title}`,
});
})
}
)
.post(
'/errorTest',
ajvValidator('json', schema, (result, c) => {
return c.json(result, 400);
return c.json(result, 400)
}),
(c) => {
const data = c.req.valid('json');
const data = c.req.valid('json')
return c.json({
success: true,
message: `${data.id} is ${data.title}`,
});
})
}
);
)

it('Should return 200 response', async () => {
const req = new Request('http://localhost/post', {
Expand All @@ -140,12 +140,12 @@ describe('With Hook', () => {
headers: {
'Content-Type': 'application/json',
},
});
const res = await app.request(req);
expect(res).not.toBeNull();
expect(res.status).toBe(200);
expect(await res.text()).toBe('123 is valid!');
});
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(await res.text()).toBe('123 is valid!')
})

it('Should return 400 response', async () => {
const req = new Request('http://localhost/post', {
Expand All @@ -157,11 +157,11 @@ describe('With Hook', () => {
headers: {
'Content-Type': 'application/json',
},
});
const res = await app.request(req);
expect(res).not.toBeNull();
expect(res.status).toBe(400);
});
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(400)
})

it('Should return 400 response and error array', async () => {
const req = new Request('http://localhost/errorTest', {
Expand All @@ -172,17 +172,17 @@ describe('With Hook', () => {
headers: {
'Content-Type': 'application/json',
},
});
const res = await app.request(req);
expect(res).not.toBeNull();
expect(res.status).toBe(400);
})
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(400)

const { errors, success } = (await res.json()) as {
success: boolean;
errors: ErrorObject[];
};
expect(success).toBe(false);
expect(Array.isArray(errors)).toBe(true);
success: boolean
errors: ErrorObject[]
}
expect(success).toBe(false)
expect(Array.isArray(errors)).toBe(true)
expect(
errors.map((e: ErrorObject) => ({
keyword: e.keyword,
Expand All @@ -195,6 +195,6 @@ describe('With Hook', () => {
instancePath: '',
message: "must have required property 'title'",
},
]);
});
});
])
})
})
3 changes: 2 additions & 1 deletion packages/arktype-validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type, type Type, type ArkErrors } from 'arktype'
import { type } from 'arktype'
import type { Type, ArkErrors } from 'arktype'
import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse } from 'hono'
import { validator } from 'hono/validator'

Expand Down
5 changes: 2 additions & 3 deletions packages/auth-js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type WindowProps = {
}

export type AuthState = {
status: 'loading' | 'success' | 'errored'
status: 'loading' | 'success' | 'errored'
error?: string
}

Expand Down Expand Up @@ -163,8 +163,7 @@ export function now() {

export function parseUrl(url?: string) {
const defaultUrl = 'http://localhost:3000/api/auth'
const parsedUrl = new URL(url?.startsWith('http') ? url : `https://${url}` || defaultUrl)

const parsedUrl = new URL(url ? (url.startsWith('http') ? url : `https://${url}`) : defaultUrl)
const path = parsedUrl.pathname === '/' ? '/api/auth' : parsedUrl.pathname.replace(/\/$/, '')
const base = `${parsedUrl.origin}${path}`

Expand Down
Loading

0 comments on commit 7a401b0

Please sign in to comment.