Skip to content

Commit

Permalink
ok for params and query
Browse files Browse the repository at this point in the history
  • Loading branch information
mingder78 committed Dec 16, 2023
1 parent d9dd6e6 commit e41fddf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"start": "bun run ./src/index.ts",
"build": "bun-vercel ./src/index.ts",
"dev": "bun --watch run ./src/index.ts",
"dev": "bun --hot run ./src/index.ts",
"test": "bun --watch test"
},
"devDependencies": {
Expand Down
32 changes: 27 additions & 5 deletions src/items/router/items.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// src/items/items.router.ts

import { cors } from '@elysiajs/cors'
//import { cors } from '@elysiajs/cors'
import { swagger } from '@elysiajs/swagger'

// interfaces

import * as ItemService from "../service/items";
import { BaseItem, Item } from "../interface/item";
import { Elysia } from "elysia";
import { Elysia, t } from "elysia";

class CustomError extends Error {
constructor(public message: string) {
Expand All @@ -17,7 +17,7 @@ class CustomError extends Error {

export const router = new Elysia()
.use(swagger())
.use(cors())
// .use(cors()) // <-- add a specific IP and service, or use jwt/auth
.onError(({ code, error }) => {
return new Response(error.toString())
})
Expand Down Expand Up @@ -49,9 +49,31 @@ export const router = new Elysia()
return { success: false };
}
})
.get("/search/:word", async ({ params: { word }, query: { testnet } }) => {
try {
return await ItemService.search(word,testnet);
} catch (e) {
return { success: false };
}
}, {
params: t.Object({
word: t.String()
}),
query: t.Object({
testnet: t.Optional(t.String())
},
{
/**
* @default false
* Accept additional properties
* that not specified in schema
* but still match the type
*/
additionalProperties: true
})})
})
.get('/', () => {
throw new CustomError('Hello Error');
})
throw new CustomError('Hello Error, use /swagger to see all APIs');
})


6 changes: 6 additions & 0 deletions src/items/service/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ export const remove = async (id: number): Promise<null | void> => {
delete items[id];
};

export const search = async (word: string, testnet: string | undefined): Promise<Item[]> => {
console.log(word)
console.log(testnet)
return [];
};

0 comments on commit e41fddf

Please sign in to comment.