Skip to content

Commit

Permalink
feat: extension improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
arpowers committed Apr 22, 2022
1 parent 6d354b6 commit 0e3ef89
Show file tree
Hide file tree
Showing 27 changed files with 352 additions and 576 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -59,7 +59,7 @@
"tslib": "^2.3.1",
"typescript": "^4.6.3",
"vite": "^2.9.5",
"vitest": "^0.9.3",
"vitest": "^0.9.4",
"vue": "^3.2.33",
"yorkie": "^2.0.0"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/api/config/plugin.ts
Expand Up @@ -40,10 +40,12 @@ export abstract class FactorPlugin<T extends Record<string, unknown> = {}> {
R extends Record<string, Query> = Record<string, Query>,
>(params: {
queries: R
serverUrl: string
serverUrl?: string
basePath?: string
endpointHandler?: typeof Endpoint
}): M {
if (!params.serverUrl) return {} as M

const { queries, serverUrl, basePath, endpointHandler = Endpoint } = params
const q = queries ?? {}

Expand Down
29 changes: 16 additions & 13 deletions packages/api/config/types.ts
Expand Up @@ -41,28 +41,31 @@ export interface UserConfig {
| undefined
| void
| Promise<UserConfig | undefined | void>
variables?: Record<
string,
| string
| number
| Record<string, string>
| string[]
| Record<string, string>[]
>

cwd?: string
root?: string
paths?: string[]
endpoints?: Endpoint[]
port?: string
portApp?: string
serverUrl?: string

serverOnlyImports?: ServerModuleDef[]
log?: LogHandler
// extend
paths?: string[]
endpoints?: Endpoint[]
routes?: AppRoute<string>[]
sitemaps?: SiteMapConfig[]
log?: LogHandler
plugins?: (UserConfig | Promise<UserConfig>)[]
hooks?: HookType<HookDictionary>[]
service?: { key: string; run: () => Promise<void> | void }[]
// build
vite?: Partial<ViteInlineConfig>
serverOnlyImports?: ServerModuleDef[]
generateStaticConfig?: boolean
variables?: Record<
string,
| string
| number
| Record<string, string>
| string[]
| Record<string, string>[]
>
}
2 changes: 1 addition & 1 deletion packages/api/entry/setupApp.ts
Expand Up @@ -89,7 +89,7 @@ export const mountApp = async (params: {
renderUrl,
} = params

if (!isNode) {
if (!isNode()) {
const entry = await factorApp({ mainFile, RootComponent, renderUrl })

initializeResetUi().catch(console.error)
Expand Down
2 changes: 1 addition & 1 deletion packages/api/index.ts
Expand Up @@ -4,7 +4,7 @@ export * from "./types"
export * from "./engine"
export * from "./config/plugin"
export * from "./config/types"
export * from "./utils/_"

export * from "./router"
export * from "./store"
export * from "./utils/error"
Expand Down
4 changes: 2 additions & 2 deletions packages/api/package.json
Expand Up @@ -53,7 +53,7 @@
"dotenv": "^16.0.0",
"enquirer": "^2.3.6",
"es-module-lexer": "^0.10.5",
"esbuild": "^0.14.36",
"esbuild": "^0.14.37",
"events": "^3.3.0",
"express": "^4.17.3",
"extract-domain": "^2.4.1",
Expand Down Expand Up @@ -102,7 +102,7 @@
"string-similarity": "^4.0.4",
"tailwindcss": "^3.0.24",
"ts-node": "^10.7.0",
"tsup": "^5.12.5",
"tsup": "^5.12.6",
"typescript": "^4.6.3",
"validator": "^13.7.0",
"vite": "^2.9.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/plugin-user/routeAuth.ts
Expand Up @@ -13,7 +13,7 @@ export const routeAuthRedirects = async (
): Promise<void> => {
// don't worry about redirects on server
// if runs in node, this hangs causing test problems
if (isNode) return
if (isNode()) return

const router = getRouter()
const currentRoute = router.currentRoute.value
Expand Down
4 changes: 3 additions & 1 deletion packages/api/render/build.ts
Expand Up @@ -17,7 +17,9 @@ const require = createRequire(import.meta.url)
export const buildApp = async (options: RunConfig): Promise<void> => {
const { prerender, dist, distClient, distServer } = options

logger.info("build", "building application", { data: { ...options, isNode } })
logger.info("build", "building application", {
data: { ...options, isNode: isNode() },
})

try {
const vc = await getViteConfig(options)
Expand Down
2 changes: 1 addition & 1 deletion packages/api/router.ts
Expand Up @@ -87,7 +87,7 @@ export class AppRoute<T extends string> {
* Creates a vue router
*/
export const createFactorRouter = (): Router => {
const history = !isNode ? createWebHistory() : createMemoryHistory()
const history = !isNode() ? createWebHistory() : createMemoryHistory()

const router = createRouter({
history,
Expand Down
15 changes: 10 additions & 5 deletions packages/api/test-utils/utils.ts
Expand Up @@ -53,12 +53,17 @@ export type TestUtils = {
}

export const createTestUtils = async (): Promise<TestUtils> => {
const serverVars = [
"POSTGRES_URL",
"GOOGLE_CLIENT_ID",
"GOOGLE_CLIENT_SECRET",
const vars = [
{ v: "POSTGRES_URL" },
{ v: "GOOGLE_CLIENT_ID" },
{ v: "GOOGLE_CLIENT_SECRET" },
] as const
const env = getEnvVars({ serverVars, isTest: true })

const env = getEnvVars({
vars,
isLive: false,
isApp: true,
})

const serverUrl = `http://localhost:${process.env.FACTOR_SERVER_PORT}`
const appUrl = `http://localhost:${process.env.FACTOR_APP_PORT}`
Expand Down
2 changes: 1 addition & 1 deletion packages/api/utils-analytics/geo.ts
Expand Up @@ -66,7 +66,7 @@ export const setUserGeolocation = async (): Promise<UserGeolocation | void> => {
*/
if (stored("geo")) {
return stored("geo")
} else if (isNode) {
} else if (isNode()) {
return
}

Expand Down
8 changes: 4 additions & 4 deletions packages/api/utils-analytics/tracking.ts
Expand Up @@ -49,7 +49,7 @@ export const onBrowserEvent = <T extends BrowserEvent>(
target?: HTMLElement | Window | Document,
): (() => void) => {
// in case on server
if (isNode) {
if (isNode()) {
return (): void => {}
}

Expand Down Expand Up @@ -176,7 +176,7 @@ export class ActivityTrigger {
* Gets the URL if set by <link ref="canonical"> tag
*/
export const canonicalUrlFromTag = (): string | undefined => {
if (isNode) return
if (isNode()) return

const tags = document.querySelectorAll("link")
for (let i = 0, tag; (tag = tags[i]); i++) {
Expand All @@ -190,7 +190,7 @@ export const canonicalUrlFromTag = (): string | undefined => {
* Return the canonical URL and remove the hash.
*/
export const getCanonicalUrl = (): string | undefined => {
if (isNode) return
if (isNode()) return

const { search = "" } = location
const canonical = canonicalUrlFromTag()
Expand Down Expand Up @@ -416,7 +416,7 @@ class UnloadHandler {

public onUnload(cb: EventCallback): void {
this.unloadCallbacks.push(cb)
if (!isNode && this.unloadWatchers.length == 0) {
if (!isNode() && this.unloadWatchers.length == 0) {
this.unloadWatchers = [
onBrowserEvent("pagehide", () => this.unload("pagehide")),
onBrowserEvent("beforeunload", () => this.unload("unload")),
Expand Down
32 changes: 18 additions & 14 deletions packages/api/utils/buffer.ts
Expand Up @@ -11,14 +11,14 @@ type BufferConfig<T = Record<string, any>> = {
}

export class WriteBuffer<T> extends EventEmitter {
name: string
items: T[]
maxSeconds: number
limit: number
limitType: "item" | "size" | "time"
flush: (items: T[]) => void | Promise<void>
intervalId?: NodeJS.Timeout
key: string
readonly name: string
readonly key: string
private items: T[]
private maxSeconds: number
private limit: number
private limitType: "item" | "size" | "time"
private flushCallback?: (items: T[]) => void | Promise<void>
private intervalId?: NodeJS.Timeout

constructor(config: BufferConfig<T>) {
super()
Expand All @@ -32,21 +32,21 @@ export class WriteBuffer<T> extends EventEmitter {
key,
} = config

this.key = key ?? "_id"
this.name = name
this.items = []
this.limit = limit ?? 5000
this.limitType = limitType ?? "item"
this.maxSeconds = maxSeconds ?? 2
this.flush = flush ?? this.onFlush
this.key = key ?? "_id"
this.flushCallback = flush

// Flush on process shutdown
onEvent("shutdown", () => this.flushBuffer())
}
/**
* Default flush
*/
protected onFlush(this: void, _items: T[]): void {}
protected flush(_items: T[]): void {}
/**
* Remove items in buffer without a flush callback
*/
Expand All @@ -62,10 +62,14 @@ export class WriteBuffer<T> extends EventEmitter {

this.stopTimeout()
// use resolve to ensure is a promise
Promise.resolve(this.flush(this.items)).catch((error) => {
console.error(error)
})
Promise.resolve(this.flush(this.items)).catch(console.error)

if (this.flushCallback) {
Promise.resolve(this.flushCallback(this.items)).catch(console.error)
}

this.emit("flush", this.items)

this.items = []
}

Expand Down
3 changes: 3 additions & 0 deletions packages/api/utils/index.ts
Expand Up @@ -2,3 +2,6 @@ export * from "./time"
export * from "./utils"
export * from "./hook"
export * from "./fetch"
export * from "./vars"
export * from "./libraries"
export * from "./_"
12 changes: 12 additions & 0 deletions packages/api/utils/libraries.ts
@@ -0,0 +1,12 @@
import express from "express"
import axios from "axios"
import * as vue from "vue"
import dayjs from "dayjs"
import relativeTime from "dayjs/plugin/relativeTime"
import utc from "dayjs/plugin/utc"
import tz from "dayjs/plugin/timezone"
dayjs.extend(relativeTime)
dayjs.extend(utc)
dayjs.extend(tz)

export { dayjs, express, axios, vue }
10 changes: 1 addition & 9 deletions packages/api/utils/time.ts
@@ -1,15 +1,7 @@
import dayjs from "dayjs"
import relativeTime from "dayjs/plugin/relativeTime"
import utc from "dayjs/plugin/utc"
import tz from "dayjs/plugin/timezone"
dayjs.extend(relativeTime)
dayjs.extend(utc)
dayjs.extend(tz)
import { dayjs } from "./libraries"

type DateTypes = string | number | Date | dayjs.Dayjs | undefined

export { dayjs }

const _isNumber = (value: DateTypes): boolean => {
if (typeof value == "undefined") return false
return !!(
Expand Down

0 comments on commit 0e3ef89

Please sign in to comment.