manifold/og-image/api/_lib/parser.ts

109 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

2022-02-18 00:34:11 +00:00
import { IncomingMessage } from 'http'
import { parse } from 'url'
import { ParsedRequest } from './types'
export function parseRequest(req: IncomingMessage) {
2022-02-18 00:34:11 +00:00
console.log('HTTP ' + req.url)
const { pathname, query } = parse(req.url || '/', true)
const {
fontSize,
images,
widths,
heights,
theme,
md,
// Attributes for Manifold card:
question,
probability,
2022-08-05 12:56:10 +00:00
numericValue,
metadata,
creatorName,
creatorUsername,
creatorAvatarUrl,
Challenge Bets (#679) * Challenge bets * Store avatar url * Fix before and after probs * Check balance before creation * Calculate winning shares * pretty * Change winning value * Set shares to equal each other * Fix share challenge link * pretty * remove lib refs * Probability of bet is set to market * Remove peer pill * Cleanup * Button on contract page * don't show challenge if not binary or if resolved * challenge button (WIP) * fix accept challenge: don't change pool/probability * Opengraph preview [WIP] * elim lib * Edit og card props * Change challenge text * New card gen attempt * Get challenge on server * challenge button styling * Use env domain * Remove other window ref * Use challenge creator as avatar * Remove user name * Remove s from property, replace prob with outcome * challenge form * share text * Add in challenge parts to template and url * Challenge url params optional * Add challenge params to parse request * Parse please * Don't remove prob * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Add to readme about how to dev og-image * Add emojis * button: gradient background, 2xl size * beautify accept bet screen * update question button * Add separate challenge template * Accepted challenge sharing card, fix accept bet call * accept challenge button * challenge winner page * create challenge screen * Your outcome/cost=> acceptorOutcome/cost * New create challenge panel * Fix main merge * Add challenge slug to bet and filter by it * Center title * Add helper text * Add FAQ section * Lint * Columnize the user areas in preview link too * Absolutely position * Spacing * Orientation * Restyle challenges list, cache contract name * Make copying easy on mobile * Link spacing * Fix spacing * qr codes! * put your challenges first * eslint * Changes to contract buttons and create challenge modal * Change titles around for current bet * Add back in contract title after winning * Cleanup * Add challenge enabled flag * Spacing of switch button * Put sharing qr code in modal Co-authored-by: mantikoros <sgrugett@gmail.com>
2022-08-04 21:27:02 +00:00
// Challenge attributes:
challengerAmount,
challengerOutcome,
creatorAmount,
creatorOutcome,
acceptedName,
acceptedAvatarUrl,
2022-02-18 00:34:11 +00:00
} = query || {}
if (Array.isArray(fontSize)) {
2022-02-18 00:34:11 +00:00
throw new Error('Expected a single fontSize')
}
if (Array.isArray(theme)) {
2022-02-18 00:34:11 +00:00
throw new Error('Expected a single theme')
}
2022-02-18 00:34:11 +00:00
const arr = (pathname || '/').slice(1).split('.')
let extension = ''
let text = ''
if (arr.length === 0) {
2022-02-18 00:34:11 +00:00
text = ''
} else if (arr.length === 1) {
2022-02-18 00:34:11 +00:00
text = arr[0]
} else {
2022-02-18 00:34:11 +00:00
extension = arr.pop() as string
text = arr.join('.')
}
// Take a url query param and return a single string
const getString = (stringOrArray: string[] | string | undefined): string => {
if (Array.isArray(stringOrArray)) {
// If the query param is an array, return the first element
2022-02-18 00:34:11 +00:00
return stringOrArray[0]
}
2022-02-18 00:34:11 +00:00
return stringOrArray || ''
}
const parsedRequest: ParsedRequest = {
2022-02-18 00:34:11 +00:00
fileType: extension === 'jpeg' ? extension : 'png',
text: decodeURIComponent(text),
2022-02-18 00:34:11 +00:00
theme: theme === 'dark' ? 'dark' : 'light',
md: md === '1' || md === 'true',
fontSize: fontSize || '96px',
images: getArray(images),
widths: getArray(widths),
heights: getArray(heights),
question:
2022-02-18 00:34:11 +00:00
getString(question) || 'Will you create a prediction market on Manifold?',
probability: getString(probability),
2022-08-05 12:56:10 +00:00
numericValue: getString(numericValue) || '',
2022-02-18 00:34:11 +00:00
metadata: getString(metadata) || 'Jan 1 &nbsp;•&nbsp; M$ 123 pool',
creatorName: getString(creatorName) || 'Manifold Markets',
creatorUsername: getString(creatorUsername) || 'ManifoldMarkets',
creatorAvatarUrl: getString(creatorAvatarUrl) || '',
Challenge Bets (#679) * Challenge bets * Store avatar url * Fix before and after probs * Check balance before creation * Calculate winning shares * pretty * Change winning value * Set shares to equal each other * Fix share challenge link * pretty * remove lib refs * Probability of bet is set to market * Remove peer pill * Cleanup * Button on contract page * don't show challenge if not binary or if resolved * challenge button (WIP) * fix accept challenge: don't change pool/probability * Opengraph preview [WIP] * elim lib * Edit og card props * Change challenge text * New card gen attempt * Get challenge on server * challenge button styling * Use env domain * Remove other window ref * Use challenge creator as avatar * Remove user name * Remove s from property, replace prob with outcome * challenge form * share text * Add in challenge parts to template and url * Challenge url params optional * Add challenge params to parse request * Parse please * Don't remove prob * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Challenge card styling * Add to readme about how to dev og-image * Add emojis * button: gradient background, 2xl size * beautify accept bet screen * update question button * Add separate challenge template * Accepted challenge sharing card, fix accept bet call * accept challenge button * challenge winner page * create challenge screen * Your outcome/cost=> acceptorOutcome/cost * New create challenge panel * Fix main merge * Add challenge slug to bet and filter by it * Center title * Add helper text * Add FAQ section * Lint * Columnize the user areas in preview link too * Absolutely position * Spacing * Orientation * Restyle challenges list, cache contract name * Make copying easy on mobile * Link spacing * Fix spacing * qr codes! * put your challenges first * eslint * Changes to contract buttons and create challenge modal * Change titles around for current bet * Add back in contract title after winning * Cleanup * Add challenge enabled flag * Spacing of switch button * Put sharing qr code in modal Co-authored-by: mantikoros <sgrugett@gmail.com>
2022-08-04 21:27:02 +00:00
challengerAmount: getString(challengerAmount) || '',
challengerOutcome: getString(challengerOutcome) || '',
creatorAmount: getString(creatorAmount) || '',
creatorOutcome: getString(creatorOutcome) || '',
acceptedName: getString(acceptedName) || '',
acceptedAvatarUrl: getString(acceptedAvatarUrl) || '',
2022-02-18 00:34:11 +00:00
}
parsedRequest.images = getDefaultImages(parsedRequest.images)
return parsedRequest
}
function getArray(stringOrArray: string[] | string | undefined): string[] {
2022-02-18 00:34:11 +00:00
if (typeof stringOrArray === 'undefined') {
return []
} else if (Array.isArray(stringOrArray)) {
2022-02-18 00:34:11 +00:00
return stringOrArray
} else {
2022-02-18 00:34:11 +00:00
return [stringOrArray]
}
}
function getDefaultImages(images: string[]): string[] {
2022-02-18 00:34:11 +00:00
const defaultImage = 'https://manifold.markets/logo.png'
if (!images || !images[0]) {
2022-02-18 00:34:11 +00:00
return [defaultImage]
}
2022-02-18 00:34:11 +00:00
return images
}