( c: Context, username: string, flags: InputFlags )
| 26 | |
| 27 | /* Handler for Twitter users */ |
| 28 | export const handleProfile = async ( |
| 29 | c: Context, |
| 30 | username: string, |
| 31 | flags: InputFlags |
| 32 | ): Promise<Response> => { |
| 33 | console.log('flags', JSON.stringify(flags)); |
| 34 | const api = await userAPI(username, twitterBuildHostFromContext(c), true); |
| 35 | const user = api?.user as APIUser; |
| 36 | |
| 37 | /* Catch this request if it's an API response */ |
| 38 | // For now we just always return the API response while testing |
| 39 | if (flags?.api) { |
| 40 | c.status(api.code as ContentfulStatusCode); |
| 41 | // Add every header from Constants.API_RESPONSE_HEADERS |
| 42 | for (const [header, value] of Object.entries(Constants.API_RESPONSE_HEADERS)) { |
| 43 | c.header(header, value); |
| 44 | } |
| 45 | return c.json(api); |
| 46 | } |
| 47 | |
| 48 | /* If there was any errors fetching the User, we'll return it */ |
| 49 | switch (api.code) { |
| 50 | case 401: |
| 51 | return returnError(c, Strings.ERROR_PRIVATE); |
| 52 | case 404: |
| 53 | return returnError( |
| 54 | c, |
| 55 | api.reason === 'suspended' ? Strings.ERROR_USER_SUSPENDED : Strings.ERROR_USER_NOT_FOUND |
| 56 | ); |
| 57 | case 500: |
| 58 | return returnError(c, Strings.ERROR_API_FAIL); |
| 59 | } |
| 60 | |
| 61 | /* Base headers included in all responses */ |
| 62 | const headers = [`<meta property="twitter:site" content="@${user.screen_name}"/>`]; |
| 63 | |
| 64 | const branding = getBranding(c); |
| 65 | const feedOrigin = new URL(c.req.url).origin; |
| 66 | const enc = encodeURIComponent(username); |
| 67 | const linkTitle = `@${username} — ${branding.name}`.replace(/"/g, '"'); |
| 68 | headers.push( |
| 69 | `<link rel="alternate" type="application/rss+xml" title="${linkTitle}" href="${feedOrigin}/${enc}/feed.xml"/>` |
| 70 | ); |
| 71 | headers.push( |
| 72 | `<link rel="alternate" type="application/atom+xml" title="${linkTitle}" href="${feedOrigin}/${enc}/feed.atom.xml"/>` |
| 73 | ); |
| 74 | |
| 75 | // TODO Add card creation logic here |
| 76 | /* Finally, after all that work we return the response HTML! */ |
| 77 | |
| 78 | return c.html( |
| 79 | Strings.BASE_HTML.format({ |
| 80 | runtime: formatRuntime(), |
| 81 | brandingName: branding.name, |
| 82 | lang: `lang="en"`, |
| 83 | headers: headers.join(''), |
| 84 | body: '' |
| 85 | }) |
no test coverage detected