Skip to content

Commit

Permalink
Merge pull request #59 from mateonunez/chore/lyra-schema-resolver
Browse files Browse the repository at this point in the history
chore(impact): implement schema-resolver plugin
  • Loading branch information
mateonunez authored Oct 17, 2022
2 parents 8520358 + cac6406 commit 21ff2b9
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 99 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
},
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"@mateonunez/lyra-schema-resolver": "^0.0.2",
"@types/node": "^18.7.3",
"@types/tap": "^15.0.7",
"@typescript-eslint/eslint-plugin": "^5.32.0",
Expand Down
1 change: 0 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ export const RESPONSE_INVALID = (url: string, statusCode: number) => {
const statusText = HTTP_STATUS[statusCode]
return `Error fetching data from ${url}: ${statusCode} ${statusText}`
}
export const UNSUPPORTED_TYPE_SCHEMA = (type: string) => `Unsupported type schema: ${type}`
40 changes: 0 additions & 40 deletions src/schema/resolver.ts

This file was deleted.

21 changes: 2 additions & 19 deletions src/tests/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {test} from "tap"
import {FILE_NOT_FOUND, MISSING_GRAPHQL_QUERY, RESPONSE_INVALID, UNSUPPORTED_CONTENT_TYPE, UNSUPPORTED_FETCHER, UNSUPPORTED_TYPE_SCHEMA} from "../errors"
import {FILE_NOT_FOUND, MISSING_GRAPHQL_QUERY, RESPONSE_INVALID, UNSUPPORTED_CONTENT_TYPE, UNSUPPORTED_FETCHER} from "../errors"
import {impact} from "../runtimes/server"
import {resolveSchema} from "../schema/resolver"

const rickAndMorty = "https://rickandmortyapi.com/api/character"
const xml = "https://www.w3schools.com/xml/simple.xml"

test("errors - runtime server", ({test, plan}) => {
plan(2)
plan(1)

test("fetchers", ({test, plan}) => {
plan(4)
Expand Down Expand Up @@ -56,20 +55,4 @@ test("errors - runtime server", ({test, plan}) => {
})
})
})

test("schema", ({test, plan}) => {
plan(1)

test("invalid schema should throw an error", ({same, end}) => {
const data = {username: "mateonunez", description: () => ({})}

try {
resolveSchema({}, data)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
same(err.message, UNSUPPORTED_TYPE_SCHEMA("function"))
end()
}
})
})
})
34 changes: 1 addition & 33 deletions src/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { create, insert, search } from "@lyrasearch/lyra"
import {test} from "tap"
import {resolveSchema} from "../schema/resolver"
import {parseData} from "../utils"

test("utils - runtime server", ({test, plan}) => {
plan(2)
plan(1)

test("parsing data", ({test, plan}) => {
plan(2)
Expand Down Expand Up @@ -117,34 +115,4 @@ test("utils - runtime server", ({test, plan}) => {
})
})
})

test("schema", ({test, plan}) => {
plan(2)

test("the schema should be resolved", ({same, end}) => {
const data = {name: "mateonunez", age: 27, is_admin: false}
const expected = {name: "string", age: "number", is_admin: "boolean"}
const schema = resolveSchema({}, data)

same(schema, expected)
end()
})

test("the schema with nested object should be resolved and searchable", ({same, end}) => {
const data = {name: "mateonunez", age: 27, is_admin: false, address: {street: "street", number: 123}}
const expected = {name: "string", age: "number", is_admin: "boolean", address: {street: "string", number: "number"}}
const schema = resolveSchema({}, data)

const lyra = create({schema})
insert(lyra, data)
const {hits} = search(lyra, {
term: "street"
})

same(schema, expected)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
same(hits.map(({id, ...rest}) => ({...rest})), [data])
end()
})
})
})
8 changes: 2 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Configuration as LyraConfiguration, create, insert, Lyra, PropertiesSche
import {UNSUPPORTED_CONTENT_TYPE} from "./errors"
import parseCsv from "./runtimes/common/parsers/csv"
import parseJson from "./runtimes/common/parsers/json"
import {resolveSchema} from "./schema/resolver"
import resolveSchema from '@mateonunez/lyra-schema-resolver'

export type ParseDataOptions = {
contentType?: string
Expand Down Expand Up @@ -39,12 +39,8 @@ export function sanitizeString(str: string): string {
return str.trim()
}

export function getMaxOfArray(array: any[]): number {
return array.reduce((max, v) => (max >= v ? max : v), -Infinity)
}

export function createLyra<T extends PropertiesSchema>(data: any, options?: Omit<LyraConfiguration<any>, "schema">): Lyra<T> {
const schema = resolveSchema({}, data)
const schema = resolveSchema(data)
const lyra = create({
schema,
...options
Expand Down

0 comments on commit 21ff2b9

Please sign in to comment.