Merge pull request #1831 from omnivore-app/feat/save-url-api
Add save API endpoint to frontend for URL based saving
This commit is contained in:
@ -2,19 +2,17 @@ import { gql } from 'graphql-request'
|
||||
import { gqlFetcher } from '../networkHelpers'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
|
||||
|
||||
type SaveLinkOutput = {
|
||||
export type SaveLinkOutput = {
|
||||
jobId?: string
|
||||
url?: string
|
||||
clientRequestId?: string
|
||||
}
|
||||
|
||||
type SaveResponseData = {
|
||||
export type SaveResponseData = {
|
||||
saveUrl?: SaveUrlData
|
||||
}
|
||||
|
||||
type SaveUrlData = {
|
||||
export type SaveUrlData = {
|
||||
id: string
|
||||
url: string
|
||||
slug: string
|
||||
@ -26,9 +24,7 @@ export async function saveUrlMutation(
|
||||
): Promise<SaveLinkOutput | undefined> {
|
||||
const clientRequestId = uuidv4()
|
||||
const mutation = gql`
|
||||
mutation SaveUrl(
|
||||
$input: SaveUrlInput!
|
||||
) {
|
||||
mutation SaveUrl($input: SaveUrlInput!) {
|
||||
saveUrl(input: $input) {
|
||||
... on SaveSuccess {
|
||||
url
|
||||
@ -48,7 +44,7 @@ export async function saveUrlMutation(
|
||||
url,
|
||||
clientRequestId,
|
||||
source: 'add-link',
|
||||
}
|
||||
},
|
||||
})
|
||||
const output = data as SaveResponseData | undefined
|
||||
return {
|
||||
@ -56,7 +52,8 @@ export async function saveUrlMutation(
|
||||
jobId: output?.saveUrl?.clientRequestId,
|
||||
clientRequestId: output?.saveUrl?.clientRequestId,
|
||||
}
|
||||
} catch {
|
||||
} catch (error) {
|
||||
console.log('error savingUrl', { error })
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
59
packages/web/pages/api/save.ts
Normal file
59
packages/web/pages/api/save.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { SaveResponseData } from '../../lib/networking/mutations/saveUrlMutation'
|
||||
import { ssrFetcher } from '../../lib/networking/networkHelpers'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
const saveUrl = async (req: NextApiRequest, url: URL) => {
|
||||
const clientRequestId = uuidv4()
|
||||
const mutation = `
|
||||
mutation SaveUrl($input: SaveUrlInput!) {
|
||||
saveUrl(input: $input) {
|
||||
... on SaveSuccess {
|
||||
url
|
||||
clientRequestId
|
||||
}
|
||||
... on SaveError {
|
||||
errorCodes
|
||||
message
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
try {
|
||||
const data = await ssrFetcher({ req }, mutation, {
|
||||
input: {
|
||||
clientRequestId,
|
||||
url: url.toString(),
|
||||
source: 'api-save-url',
|
||||
},
|
||||
})
|
||||
|
||||
const output = data as SaveResponseData | undefined
|
||||
return {
|
||||
url: output?.saveUrl?.url,
|
||||
jobId: output?.saveUrl?.clientRequestId,
|
||||
clientRequestId: output?.saveUrl?.clientRequestId,
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('error savingUrl', { error })
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line import/no-anonymous-default-export
|
||||
export default async (
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
): Promise<void> => {
|
||||
const urlStr = req.query['url']
|
||||
const url = new URL(urlStr as string)
|
||||
const saveResult = await saveUrl(req, url)
|
||||
console.log('saveResult: ', saveResult)
|
||||
if (saveResult?.jobId) {
|
||||
res.redirect(`/sr/${saveResult?.jobId}`)
|
||||
return
|
||||
}
|
||||
|
||||
res.status(200).send('ok')
|
||||
}
|
||||
Reference in New Issue
Block a user