Skip to content

Commit 0f808ab

Browse files
committed
WIP: write&edit api migration
1 parent 52abaa4 commit 0f808ab

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

src/services/postService.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WritePostResponse } from './../../../velog-client/src/lib/graphql/post';
12
import { GetPostsByTagParams } from './../entity/PostsTags';
23
import { Post, PostTag, Tag, User } from '@prisma/client';
34
import db from '../lib/db';
@@ -321,6 +322,90 @@ const postService = {
321322
console.log('error', error);
322323
}
323324
},
325+
326+
async write(args: WritePostArgs, cookies: Cookies) {
327+
const WRITE_POST_MUTATION = `
328+
mutation WritePost($input: WritePostInput!) {
329+
writePost(input: $input) {
330+
id
331+
user {
332+
id
333+
username
334+
}
335+
url_slug
336+
}
337+
}
338+
`;
339+
340+
const endpoint = getEndpoint();
341+
const accessToken = cookies.get('access_token') ?? '';
342+
343+
try {
344+
const res = await Axios.post<AxiosResponse<WritePostResponse>>(
345+
endpoint,
346+
{
347+
operationName: 'WritePost',
348+
query: WRITE_POST_MUTATION,
349+
variables: {
350+
input: {
351+
...args,
352+
},
353+
},
354+
},
355+
{
356+
headers: {
357+
'Content-Type': 'application/json',
358+
authorization: `Bearer ${accessToken}`,
359+
},
360+
}
361+
);
362+
363+
return res.data.data.writePost;
364+
} catch (error) {
365+
console.log('error', error);
366+
}
367+
},
368+
async editPost(args: EditPostArgs, cookies: Cookies) {
369+
const EDIT_POST_MUTATION = `
370+
mutation EditPost($input: EditPostInput!) {
371+
editPost(input: $input) {
372+
id
373+
user {
374+
id
375+
username
376+
}
377+
url_slug
378+
}
379+
}
380+
`;
381+
382+
const endpoint = getEndpoint();
383+
const accessToken = cookies.get('access_token') ?? '';
384+
try {
385+
const res = await Axios.post<AxiosResponse<EditPostResponse>>(
386+
endpoint,
387+
{
388+
operationName: 'EditPost',
389+
query: EDIT_POST_MUTATION,
390+
variables: {
391+
input: {
392+
...args,
393+
},
394+
},
395+
},
396+
{
397+
headers: {
398+
'Content-Type': 'application/json',
399+
authorization: `Bearer ${accessToken}`,
400+
},
401+
}
402+
);
403+
404+
return res.data.data.editPost;
405+
} catch (error) {
406+
console.log('error', error);
407+
}
408+
},
324409
};
325410

326411
export default postService;
@@ -359,3 +444,43 @@ type UnlikePostResponse = {
359444
likes: number;
360445
};
361446
};
447+
448+
type WritePostArgs = {
449+
title: string;
450+
body: string;
451+
tags: string[];
452+
is_markdown: boolean;
453+
is_temp: boolean;
454+
url_slug: string;
455+
thumbnail: string | null;
456+
meta: any;
457+
series_id?: string;
458+
is_private: boolean;
459+
token: string | null;
460+
};
461+
462+
type EditPostArgs = WritePostArgs & {
463+
id: string;
464+
};
465+
466+
type WritePostResponse = {
467+
writePost: {
468+
id: string;
469+
user: {
470+
id: string;
471+
username: string;
472+
};
473+
url_slug: string;
474+
};
475+
};
476+
477+
type EditPostResponse = {
478+
editPost: {
479+
id: string;
480+
user: {
481+
id: string;
482+
username: string;
483+
};
484+
url_slug: string;
485+
};
486+
};

0 commit comments

Comments
 (0)