diff --git a/bun.lockb b/bun.lockb index 99143a7..b08b250 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..ab5afb2 --- /dev/null +++ b/example/.gitignore @@ -0,0 +1,176 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +\*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +\*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +\*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +\*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.cache +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +.cache/ + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp +.cache + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.\* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store + diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..1361aff --- /dev/null +++ b/example/README.md @@ -0,0 +1,15 @@ +# example + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run index.ts +``` + +This project was created using `bun init` in bun v1.0.10. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/example/bun.lockb b/example/bun.lockb new file mode 100755 index 0000000..0ce0331 Binary files /dev/null and b/example/bun.lockb differ diff --git a/example/package.json b/example/package.json new file mode 100644 index 0000000..633de7b --- /dev/null +++ b/example/package.json @@ -0,0 +1,18 @@ +{ + "name": "example", + "module": "index.ts", + "type": "module", + "scripts": { + "dev": "bun --watch run ./src/index.ts", + "test": "bun --watch test" + }, + "devDependencies": { + "bun-types": "latest" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "@muzamint/erica-items-db": "^0.0.0" + } +} diff --git a/example/src/index.ts b/example/src/index.ts new file mode 100644 index 0000000..be7cce6 --- /dev/null +++ b/example/src/index.ts @@ -0,0 +1,8 @@ +console.log("Hello via Bun!"); +import { router } from '@muzamint/erica-items-db' + +export default { + async fetch(request: Request, server: Server) { + return router.handle(request) + }, +} diff --git a/example/tsconfig.json b/example/tsconfig.json new file mode 100644 index 0000000..7556e1d --- /dev/null +++ b/example/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "lib": ["ESNext"], + "module": "esnext", + "target": "esnext", + "moduleResolution": "bundler", + "moduleDetection": "force", + "allowImportingTsExtensions": true, + "noEmit": true, + "composite": true, + "strict": true, + "downlevelIteration": true, + "skipLibCheck": true, + "jsx": "react-jsx", + "allowSyntheticDefaultImports": true, + "forceConsistentCasingInFileNames": true, + "allowJs": true, + "types": [ + "bun-types" // add Bun global + ] + } +} diff --git a/package.json b/package.json index cd5329f..6bb42f5 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,13 @@ { - "name": "pkg-name", - "version": "0.0.0", + "name": "@muzamint/erica-items-db", + "version": "0.0.1", "main": "dist/index.js", "types": "dist/index.d.ts", "description": "", "scripts": { "build": "bun run build.mjs", + "dev": "bun --watch run ./src/index.ts", + "test": "bun --watch test", "prepublishOnly": "bun run build" }, "files": [ @@ -26,5 +28,11 @@ "bun-plugin-dts": "^0.2.1", "bun-types": "^1.0.7", "typescript": "^5.2.2" + }, + "dependencies": { + "@elysiajs/cors": "^0.7.2", + "@elysiajs/swagger": "^0.7.4", + "elysia": "^0.7.30", + "viem": "^1.19.13" } } diff --git a/src/index.ts b/src/index.ts index e1c8f2c..efdc451 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,22 @@ export const one = 1 export const two = 2 +import { http, createPublicClient, webSocket } from 'viem' +import { mainnet } from 'viem/chains' +export * from './items' + +const client = createPublicClient({ + chain: mainnet, + transport: http(), +}) + +const webSocketClient = createPublicClient({ + chain: mainnet, + transport: webSocket( + 'wss://eth-mainnet.g.alchemy.com/v2/4iIl6mDHqX3GFrpzmfj2Soirf3MPoAcH', + ), +}) + +const bn = await client.getBlockNumber() +const bn2 = await webSocketClient.getBlockNumber() +console.log({ bn, bn2 }) + diff --git a/src/items/index.ts b/src/items/index.ts new file mode 100644 index 0000000..00ac5cd --- /dev/null +++ b/src/items/index.ts @@ -0,0 +1,4 @@ +export * from './router/items.js' +export * from './service/items.js' +export * from './interface/items.js' +export * from './interface/item.js' \ No newline at end of file diff --git a/src/items/interface/item.ts b/src/items/interface/item.ts new file mode 100644 index 0000000..5cfda58 --- /dev/null +++ b/src/items/interface/item.ts @@ -0,0 +1,12 @@ +// src/items/item.interface.ts + +export interface BaseItem { + name: string; + price: number; + description: string; + image: string; +} + +export interface Item extends BaseItem { + id: number; +} diff --git a/src/items/interface/items.ts b/src/items/interface/items.ts new file mode 100644 index 0000000..c2187e4 --- /dev/null +++ b/src/items/interface/items.ts @@ -0,0 +1,7 @@ +// src/items/items.interface.ts + +import { Item } from "./item"; + +export interface Items { + [key: number]: Item; +} diff --git a/src/items/package.json b/src/items/package.json new file mode 100644 index 0000000..67a0269 --- /dev/null +++ b/src/items/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "types": "../_types/items/index.d.ts", + "module": "../_esm/items/index.js", + "main": "../_cjs/items/index.js" +} diff --git a/src/items/router/items.ts b/src/items/router/items.ts new file mode 100644 index 0000000..da7f04b --- /dev/null +++ b/src/items/router/items.ts @@ -0,0 +1,50 @@ +// src/items/items.router.ts + +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"; + +class CustomError extends Error { + constructor(public message: string) { + super(message) + } +} + +export const router = new Elysia() + .use(swagger()) + .use(cors()) + .onError(({ code, error }) => { + return new Response(error.toString()) + }) + .get('/ping', () => 'pong') + .group("/items", app => { + return app + .get('/', async () => await ItemService.findAll() ) + .post('/', async ({ body, set }) => { + const newItem: Item = await ItemService.create(body as BaseItem); + set.status = 200 + return new Response(newItem as any, { + headers: { + 'Content-Type': 'application/json' + } + }) + }) + .delete("/:id", async ({ params }) => { + try { + await ItemService.remove(parseInt(params.id)); + return { success: true }; + } catch (e) { + return { success: false }; + } + }) + }) + .get('/', () => { + throw new CustomError('Hello Error'); + }) + + diff --git a/src/items/service/items.ts b/src/items/service/items.ts new file mode 100644 index 0000000..2c604f5 --- /dev/null +++ b/src/items/service/items.ts @@ -0,0 +1,75 @@ +// src/items/items.service.ts + +// data model interfaces + +import { BaseItem, Item } from "../interface/item"; +import { Items } from "../interface/items"; + +// in memory store + +let items: Items = { + 1: { + id: 1, + name: "Burger", + price: 599, + description: "Tasty", + image: "https://cdn.auth0.com/blog/whatabyte/burger-sm.png" + }, + 2: { + id: 2, + name: "Pizza", + price: 299, + description: "Cheesy", + image: "https://cdn.auth0.com/blog/whatabyte/pizza-sm.png" + }, + 3: { + id: 3, + name: "Tea", + price: 199, + description: "Informative", + image: "https://cdn.auth0.com/blog/whatabyte/tea-sm.png" + } +}; + +// services + +export const findAll = async (): Promise => Object.values(items); + +export const find = async (id: number): Promise => items[id]; + +export const create = async (newItem: BaseItem): Promise => { + const id = new Date().valueOf(); + + items[id] = { + id, + ...newItem, + }; + + return items[id]; +}; + +export const update = async ( + id: number, + itemUpdate: BaseItem +): Promise => { + const item = await find(id); + + if (!item) { + return null; + } + + items[id] = { id, ...itemUpdate }; + + return items[id]; +}; + +export const remove = async (id: number): Promise => { + const item = await find(id); + + if (!item) { + return null; + } + + delete items[id]; +}; + diff --git a/src/types/items.ts b/src/types/items.ts new file mode 100644 index 0000000..e69de29 diff --git a/test/index.test.ts b/test/index.test.ts index ecd2e9e..1352364 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'bun:test' -import { one, two } from '../src' +import { one, two, router } from '../src' describe('should', () => { it('export 1', () => {