Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/more biome migration #323

Merged
merged 10 commits into from
Sep 4, 2024
Merged
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
2 changes: 0 additions & 2 deletions packages/app/api-common/.eslintignore

This file was deleted.

11 changes: 0 additions & 11 deletions packages/app/api-common/.eslintrc.json

This file was deleted.

2 changes: 0 additions & 2 deletions packages/app/api-common/.prettierignore

This file was deleted.

93 changes: 45 additions & 48 deletions packages/app/api-common/package.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
{
"name": "@lokalise/api-common",
"version": "3.2.5",
"files": [
"dist"
],
"license": "Apache-2.0",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"enableTransparentWorkspaces": "false",
"homepage": "https://github.com/lokalise/shared-ts-libs",
"repository": {
"type": "git",
"url": "git://github.com/lokalise/shared-ts-libs.git"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"scripts": {
"build": "rimraf dist && vite build",
"dev": "vite watch",
"clean": "rimraf dist .eslintcache",
"lint": "eslint --cache --max-warnings=0 . && prettier --check --log-level warn src test \"**/*.{json,md}\" && tsc --noEmit",
"lint:fix": "eslint . --fix && prettier --write src test \"**/*.{json,md}\"",
"test:ci": "vitest run --coverage",
"prepublishOnly": "npm run build",
"package-version": "echo $npm_package_version"
},
"peerDependencies": {
"zod": "^3.23.8"
},
"devDependencies": {
"@lokalise/eslint-config": "latest",
"@lokalise/prettier-config": "latest",
"@lokalise/package-vite-config": "latest",
"@vitest/coverage-v8": "^2.0.4",
"prettier": "3.3.3",
"rimraf": "^5.0.5",
"typescript": "5.5.4",
"vite": "5.4.2",
"vitest": "^2.0.4"
},
"prettier": "@lokalise/prettier-config"
"name": "@lokalise/api-common",
"version": "3.2.5",
"files": ["dist"],
"license": "Apache-2.0",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"enableTransparentWorkspaces": "false",
"homepage": "https://github.com/lokalise/shared-ts-libs",
"repository": {
"type": "git",
"url": "git://github.com/lokalise/shared-ts-libs.git"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"scripts": {
"build": "rimraf dist && vite build",
"dev": "vite watch",
"clean": "rimraf dist .eslintcache",
"lint": "biome check . && tsc --project tsconfig.lint.json --noEmit",
"lint:fix": "biome check --write",
"test:ci": "vitest run --coverage",
"prepublishOnly": "npm run build",
"package-version": "echo $npm_package_version",
"postversion": "biome check --write package.json"
},
"peerDependencies": {
"zod": "^3.23.8"
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@lokalise/biome-config": "^1.4.0",
"@lokalise/package-vite-config": "latest",
"@vitest/coverage-v8": "^2.0.4",
"rimraf": "^5.0.5",
"typescript": "5.5.4",
"vite": "5.4.2",
"vitest": "^2.0.4"
}
}
72 changes: 36 additions & 36 deletions packages/app/api-common/src/apiSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,69 @@ import type { RefinementCtx } from 'zod/lib/types'
import { decodeCursor } from './cursorCodec'

export const MANDATORY_PAGINATION_CONFIG_SCHEMA = z.object({
limit: z.coerce.number().gt(0),
before: z.string().min(1).optional(),
after: z.string().min(1).optional(),
limit: z.coerce.number().gt(0),
before: z.string().min(1).optional(),
after: z.string().min(1).optional(),
})
export type MandatoryPaginationParams = z.infer<typeof MANDATORY_PAGINATION_CONFIG_SCHEMA>

export const OPTIONAL_PAGINATION_CONFIG_SCHEMA = MANDATORY_PAGINATION_CONFIG_SCHEMA.partial({
limit: true,
limit: true,
})
export type OptionalPaginationParams = z.infer<typeof OPTIONAL_PAGINATION_CONFIG_SCHEMA>

const decodeCursorHook = (value: string | undefined, ctx: RefinementCtx) => {
if (!value) {
return undefined
}
if (!value) {
return undefined
}

const result = decodeCursor(value)
if (result.result) {
return result.result
}
ctx.addIssue({
message: 'Invalid cursor',
code: z.ZodIssueCode.custom,
params: { message: result.error.message },
})
const result = decodeCursor(value)
if (result.result) {
return result.result
}
ctx.addIssue({
message: 'Invalid cursor',
code: z.ZodIssueCode.custom,
params: { message: result.error.message },
})
}

export const multiCursorMandatoryPaginationSchema = <CursorType extends z.ZodSchema>(
cursorType: CursorType,
cursorType: CursorType,
) => {
const cursor = z.string().transform(decodeCursorHook).pipe(cursorType.optional()).optional()
return z.object({
limit: z.coerce.number().gt(0),
before: cursor,
after: cursor,
})
const cursor = z.string().transform(decodeCursorHook).pipe(cursorType.optional()).optional()
return z.object({
limit: z.coerce.number().gt(0),
before: cursor,
after: cursor,
})
}
export const multiCursorOptionalPaginationSchema = <CursorType extends z.ZodSchema>(
cursorType: CursorType,
cursorType: CursorType,
) => multiCursorMandatoryPaginationSchema(cursorType).partial({ limit: true })

export const zMeta = z.object({
count: z.number(),
cursor: z.string().optional().describe('Pagination cursor, a last item id from this result set'),
hasMore: z.boolean().optional().describe('Whether there are more items to fetch'),
count: z.number(),
cursor: z.string().optional().describe('Pagination cursor, a last item id from this result set'),
hasMore: z.boolean().optional().describe('Whether there are more items to fetch'),
})

export type PaginationMeta = z.infer<typeof zMeta>

export const paginatedResponseSchema = <T extends z.ZodSchema>(dataSchema: T) =>
z.object({
data: z.array(dataSchema),
meta: zMeta,
})
z.object({
data: z.array(dataSchema),
meta: zMeta,
})
export type PaginatedResponse<T extends Record<string, unknown>> = {
data: T[]
meta: PaginationMeta
data: T[]
meta: PaginationMeta
}

export const COMMON_ERROR_RESPONSE_SCHEMA = z.object({
message: z.string(),
errorCode: z.string(),
details: z.any().optional(),
message: z.string(),
errorCode: z.string(),
details: z.any().optional(),
})

export type CommonErrorResponse = z.infer<typeof COMMON_ERROR_RESPONSE_SCHEMA>
34 changes: 17 additions & 17 deletions packages/app/api-common/src/cursorCodec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { isObject } from './typeUtils'

type Left<T> = {
error: T
result?: never
error: T
result?: never
}

type Right<U> = {
error?: never
result: U
error?: never
result: U
}

export type Either<T, U> = NonNullable<Left<T> | Right<U>>

export const encodeCursor = (object: Record<string, unknown>): string =>
Buffer.from(JSON.stringify(object)).toString('base64url')
Buffer.from(JSON.stringify(object)).toString('base64url')

export const decodeCursor = (value: string): Either<Error, Record<string, unknown>> => {
let error: unknown
try {
const decoded = Buffer.from(value, 'base64url').toString('utf-8')
const result: unknown = JSON.parse(decoded)
if (result && isObject(result)) {
return { result }
}
} catch (e) {
error = e
}
let error: unknown
try {
const decoded = Buffer.from(value, 'base64url').toString('utf-8')
const result: unknown = JSON.parse(decoded)
if (result && isObject(result)) {
return { result }
}
} catch (e) {
error = e
}

/* v8 ignore next */
return { error: error instanceof Error ? error : new Error('Invalid cursor') }
/* v8 ignore next */
return { error: error instanceof Error ? error : new Error('Invalid cursor') }
}
Loading
Loading