deploy to gcloud

This commit is contained in:
polv committed 2020-12-16 14:13:51 +07:00
1 parent e4e364ae2a
commit 4b7252c15f
12 files changed
+190 -121

No files matched your search

-72
View File
@@ -1,75 +1,3 @@
import type { ITheme } from '~/server/theme'
export const THEME: ITheme = JSON.parse(process.env.THEME!)
export async function api<T = any>(
url: string,
opts: RequestInit & {
params?: Record<string, any>
} = {}
): Promise<
| T
| {
error: {
statusCode: number
message: unknown
}
}
> {
if (!url.startsWith('/api')) {
return {
error: {
statusCode: 500,
message: 'malformed API route',
},
}
}
const baseURL = new URL(
typeof location !== 'undefined'
? location.origin
: `http://localhost:${process.env.PORT}`
)
baseURL.pathname = url
Object.entries(opts.params || {}).map(([k, v]) => {
if (v) {
/**
* Arrays will be comma separated, and empty strings will be discarded.
*/
baseURL.searchParams.set(k, v)
}
})
const r = await fetch(baseURL.href, opts)
try {
/**
* Does not rely on `r.ok`, but rather, errorhandler
*/
const data = await r.json()
/**
* Transform to Nuxt-compatible style
*/
if (data.error) {
return {
error: {
statusCode: r.status,
message: data,
},
}
}
return data
} catch (e) {
// eslint-disable-next-line no-console
console.error(e)
}
return {
error: {
statusCode: r.status,
message: r,
},
}
}