Skip to content

Commit

Permalink
refactor: bump and renew (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Jan 12, 2024
1 parent 88aa0e9 commit ecc1e21
Show file tree
Hide file tree
Showing 16 changed files with 3,209 additions and 1,718 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@hono/eslint-config'],
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"deno.enable": false,
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
testMatch: ['**/test/**/*.+(ts|tsx)', '**/validation/**/*.+(ts|tsx)'],
transform: {
'^.+\\.(ts|tsx)$': 'esbuild-jest',
Expand All @@ -9,5 +9,6 @@ module.exports = {
testEnvironment: 'miniflare',
testEnvironmentOptions: {
sitePath: './mock',
module: true,
},
}
25 changes: 16 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@
"name": "ramen-api",
"version": "0.0.1",
"main": "dist/index.js",
"type": "module",
"scripts": {
"test": "jest --verbose",
"validate": "jest ./validation/* --verbose",
"build": "esbuild --bundle --sourcemap --minify --outdir=dist ./src/index.ts",
"dev": "miniflare --live-reload --debug -s ./content",
"deploy": "wrangler publish"
"dev": "wrangler dev --live-reload src/index.ts",
"deploy": "wrangler deploy --minify src/index.ts",
"tail": "wrangler tail"
},
"license": "MIT",
"dependencies": {
"@honojs/graphql-server": "^0.1.0",
"@hono/graphql-server": "^0.4.1",
"graphql": "^16.6.0",
"hono": "^2.2.5"
"hono": "^3.12.3"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.16.0",
"@cloudflare/workers-types": "^4.20231218.0",
"@hono/eslint-config": "^0.0.3",
"@jest/expect": "^29.1.2",
"@types/jest": "^29.1.2",
"esbuild": "^0.15.10",
"esbuild-jest": "^0.5.0",
"eslint": "^8.56.0",
"jest": "^29.1.2",
"jest-environment-miniflare": "^2.10.0",
"jest-environment-miniflare": "^2.14.1",
"miniflare": "^2.10.0",
"wrangler": "^2.1.11"
"typescript": "^5.3.3",
"wrangler": "^3.22.4"
},
"engines": {
"node": ">=18"
}
}
}
70 changes: 49 additions & 21 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { getContentFromKVAsset } from 'hono/utils/cloudflare'
import type { KVNamespace } from '@cloudflare/workers-types'
import type { Context } from 'hono'
import { getContentFromKVAsset } from './workers-utils'

declare const ENV_BASE_URL: string
export const BASE_URL = ENV_BASE_URL ?? 'http://localhost:8787/'
export const BASE_URL = 'http://localhost/'

export type Env = {
Variables: {
BASE_URL: string
}
Bindings: {
__STATIC_CONTENT: KVNamespace
}
}

export type Options = {
c: Context<Env>
}

export type Photo = {
name: string
Expand Down Expand Up @@ -54,13 +68,14 @@ type ParametersWithPager = {
}

export const listShopsWithPager = async (
params: ParametersWithPager
params: ParametersWithPager,
options: Options
): Promise<listShopsWithPagerResult> => {
let { page = 1, perPage = 10 } = params
if (perPage > 100) perPage = 100
const limit = perPage
const offset = (page - 1) * perPage
const result = await listShops({ limit, offset })
const result = await listShops({ limit, offset }, options)
const totalCount = result.totalCount
const lastPage =
totalCount % perPage == 0
Expand All @@ -83,10 +98,14 @@ export const listShopsWithPager = async (
}

export const listShops = async (
params: Parameters = {}
params: Parameters = {},
options: Options
): Promise<listShopsResult> => {
const { limit = 10, offset = 0 } = params
const buffer = await getContentFromKVAsset('shops.json')
const c = options.c
const buffer = await getContentFromKVAsset('shops.json', {
namespace: c.env ? c.env.__STATIC_CONTENT : undefined,
})
const data = arrayBufferToJSON(buffer)

const shopIdsAll = data['shopIds']
Expand All @@ -98,15 +117,18 @@ export const listShops = async (

const shops = await Promise.all(
shopIds.map(async (id: string) => {
const shop = await getShop(id)
const shop = await getShop(id, options)
return shop
})
)
return { shops, totalCount }
}

export const findIndexFromId = async (id: string): Promise<number> => {
const list = await listShops()
export const findIndexFromId = async (
id: string,
options: Options
): Promise<number> => {
const list = await listShops({}, options)
const shops = list.shops
let index = 0
const matchShop = shops.filter((shop, i) => {
Expand All @@ -118,17 +140,20 @@ export const findIndexFromId = async (id: string): Promise<number> => {
if (matchShop.length > 0) return index
}

export const getShop = async (id: string): Promise<Shop> => {
export const getShop = async (id: string, options: Options): Promise<Shop> => {
let shop: Shop
try {
const buffer = await getContentFromKVAsset(`shops/${id}/info.json`)
const c = options.c
const buffer = await getContentFromKVAsset(`shops/${id}/info.json`, {
namespace: c.env ? c.env.__STATIC_CONTENT : undefined,
})
shop = arrayBufferToJSON(buffer)
} catch (e) {
throw new Error(`"shops/${id}/info.json" is not found: ${e}`)
}
if (!shop) return
shop.photos?.map((photo: Photo) => {
photo.url = fixPhotoURL({ shopId: id, path: photo.name })
photo.url = fixPhotoURL({ shopId: id, path: photo.name }, options)
})
return shop
}
Expand All @@ -145,15 +170,18 @@ export const getAuthor = async (id: string): Promise<Author> => {
return author
}

const fixPhotoURL = ({
shopId,
path,
}: {
shopId: string
path: string
}): string => {
const fixPhotoURL = (
{
shopId,
path,
}: {
shopId: string
path: string
},
options: Options
): string => {
if (path.match(/^https?:\/\/.+/)) return path
return `${BASE_URL}images/${shopId}/${path}`
return `${options.c.var.BASE_URL}images/${shopId}/${path}`
}

const arrayBufferToJSON = (arrayBuffer: ArrayBuffer) => {
Expand Down
Loading

0 comments on commit ecc1e21

Please sign in to comment.