Skip to content

Commit

Permalink
mny
Browse files Browse the repository at this point in the history
  • Loading branch information
semyenov committed Mar 16, 2024
1 parent edd0cbc commit 6e2a6b2
Show file tree
Hide file tree
Showing 24 changed files with 281 additions and 362 deletions.
11 changes: 5 additions & 6 deletions compose-dev.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
services:
app:
entrypoint:
- sleep
- infinity
- sleep
- infinity
image: docker/dev-environments-javascript:stable-1
init: true
volumes:
- type: bind
source: /var/run/docker.sock
target: /var/run/docker.sock

- type: bind
source: /var/run/docker.sock
target: /var/run/docker.sock
28 changes: 17 additions & 11 deletions lib/ajv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { basename } from 'node:path'

import { TRPCError } from '@trpc/server'
import Ajv from 'ajv'
import localize from 'ajv-i18n/localize/ru'
import ajvErrors from 'ajv-errors'
import ajvFormats from 'ajv-formats'
import ajvI18n from 'ajv-i18n'
import ajvKeywords from 'ajv-keywords'
import consola from 'consola'
import glob from 'fast-glob'

Expand All @@ -26,8 +29,12 @@ export async function createAjv() {
messages: false,
verbose: true,
allowDate: true,
parseDate: true,
addUsedSchema: true,
validateFormats: true,
inlineRefs: true,
passContext: true,
timestamp: 'date',
})

const files = await glob('*.json', {
Expand All @@ -43,27 +50,26 @@ export async function createAjv() {
return { ...schema, $id: schemaId }
}))

ajvKeywords(ajv)
ajvFormats(ajv)
ajvErrors(ajv)

ajv.addSchema(schemas)
ajv.addSchema(userSchema, 'User')
// const test = ajv.getSchema('User')
// logger.info(test?.schemaEnv)

function validateSchema(schemaId: string, data: unknown) {
const valid = ajv.validate(schemaId, data)

if (!valid) {
localize(ajv.errors)
ajvI18n.ru(ajv.errors)

throw new TRPCError({
cause: ajv.errors,
code: 'BAD_REQUEST',
message: ajv.errorsText(
ajv.errors,
{
separator: '\n',
dataVar: 'data',
},
),
message: ajv.errorsText(ajv.errors, {
separator: '\n',
dataVar: 'data',
}),
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions lib/ajv/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { TestUserInfo } from 'types'
export const userSchema: JSONSchemaType<{
status: string
info?: TestUserInfo
date: string
}> = {
type: 'object',
properties: {
Expand All @@ -13,6 +14,10 @@ export const userSchema: JSONSchemaType<{
info: {
$ref: 'TestUserInfo',
},
date: {
type: 'string',
format: 'date-time',
},
},
required: ['status'],
additionalProperties: false,
Expand Down
28 changes: 0 additions & 28 deletions lib/guard/index.ts

This file was deleted.

7 changes: 0 additions & 7 deletions lib/guard/keys.ts

This file was deleted.

29 changes: 0 additions & 29 deletions lib/guard/t1.ts

This file was deleted.

54 changes: 0 additions & 54 deletions lib/guard/t2.ts

This file was deleted.

24 changes: 0 additions & 24 deletions lib/guard/test.ts

This file was deleted.

8 changes: 4 additions & 4 deletions lib/jose/keys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFile } from 'node:fs/promises'

import * as jose from 'jose'
import { createLocalJWKSet, importJWK } from 'jose'

import type { KeyPair } from './types'

Expand All @@ -13,10 +13,10 @@ export const keys2: KeyPair = JSON.parse(await readFile(
'utf8',
))

export const keys1Private = await jose.importJWK(keys1.privateKey)
export const keys2Private = await jose.importJWK(keys2.privateKey)
export const keys1Private = await importJWK(keys1.privateKey)
export const keys2Private = await importJWK(keys2.privateKey)

export const jwks = jose.createLocalJWKSet({
export const jwks = createLocalJWKSet({
keys: [
keys1.publicKey,
keys2.publicKey,
Expand Down
24 changes: 12 additions & 12 deletions lib/jose/sign.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as jose from 'jose'
import { SignJWT, importJWK, jwtVerify } from 'jose'

import type { KeyPair } from './types'
import type { JWTPayload, JWTVerifyGetKey, VerifyOptions } from 'jose'

const alg = 'ES256'
const options = {
Expand All @@ -10,29 +11,28 @@ const options = {

export async function sign(
keyPair: KeyPair,
payload: jose.JWTPayload,
payload: JWTPayload,
) {
const { privateKey } = keyPair
const keys1Private = await jose.importJWK(privateKey)
const { kid } = privateKey

return new jose.SignJWT(payload)
const signKey = await importJWK(privateKey)

return new SignJWT(payload)
.setIssuer(options.issuer)
.setAudience(options.audience)
.setProtectedHeader({
alg,
kid: 'key1',
})
.setProtectedHeader({ alg, kid })
.setExpirationTime('10m')
.setIssuedAt()
.sign(keys1Private)
.sign(signKey)
}

export async function verify(
jwt: string,
keySet: jose.JWTVerifyGetKey,
verifyOptions?: jose.VerifyOptions,
keySet: JWTVerifyGetKey,
verifyOptions?: VerifyOptions,
) {
const { payload } = await jose.jwtVerify(
const { payload } = await jwtVerify(
jwt,
keySet,
verifyOptions,
Expand Down
1 change: 0 additions & 1 deletion lib/mongo/index.ts → lib/mongodb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { MongoClient } from 'mongodb'
const logger = consola.withTag('mongodb')
export async function createMongoDBStore() {
const client = new MongoClient('mongodb://root:example@mongodb:27017')

await client.connect()

const db = client.db('testSozdev')
Expand Down
30 changes: 11 additions & 19 deletions lib/quicktype/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'fast-glob'
import {
FetchingJSONSchemaStore,
InputData,
Expand All @@ -7,37 +6,30 @@ import {
} from 'quicktype-core'

import type {
JSONSchemaSourceData,
Options,
TargetLanguage,
} from 'quicktype-core'

export interface IQucktypeData {
typeName: string
jsonString: string
}

export async function quicktypeMultipleJSONSchema(
targetLanguage: string | TargetLanguage,
data: IQucktypeData[],
lang: string | TargetLanguage,
data: JSONSchemaSourceData[],
options: Omit<Partial<Options>, 'inputData'>,
) {
const inputData = new InputData()

const jsonInput = new JSONSchemaInput(new FetchingJSONSchemaStore())
await Promise.all(
data.map(({ name, schema }) => jsonInput.addSource({
name,
schema,
})),
)

for (const { typeName, jsonString } of data) {
await jsonInput.addSource({
name: typeName,
schema: jsonString,
})
}

const inputData = new InputData()
inputData.addInput(jsonInput)

return quicktypeMultiFile({
lang,
inputData,
lang: targetLanguage,
outputFilename: 'index',
...options,
})
}
10 changes: 4 additions & 6 deletions lib/superjson/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import superjson from 'superjson'
import json from 'superjson'

import type { DataTransformer } from '@trpc/server'
import type { DataTransformerOptions } from '@trpc/server'

export const transformer: DataTransformer = {
serialize: (obj: unknown) => superjson.stringify(obj),
deserialize: (obj: string) => superjson.parse(obj),
}
export const superjson = json as DataTransformerOptions
export const transformer = undefined
Loading

0 comments on commit 6e2a6b2

Please sign in to comment.