import { PostsManagePayload, Post, PostsRefreshPayload, ResponseError, ConfigSavePayload, WPPost, GetDataFromURLPayload, SaveApiKeyPayload, SaveApiKeyResponse, PostsRefreshResponse, SaveConfigPayload, SyncPostsPayload, LinkPostPayload, UnlinkPostPayload, PinPostPayload, UnpinPostPayload, WPOptionsSettings, RefreshPlanResponse } from "@app/models/api"; import { decode } from "html-entities"; const config = process.env.CONFIG as any; const baseURL = config?.baseUrl; export const updateSfSyncSettings = async ( settings: WPOptionsSettings, ): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + "/sfsync/v1/settings", { method: "POST", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify(settings), } ); // This is to handle the case where the response is not JSON let data = null; try { data = await response.json(); } catch (error: any) { return { error: true, status: response.status, message: null, }; } if (!response.ok) { return { error: true, status: response.status, message: data?.message, }; } else { return data; } } catch (error: any) { return { error: true, status: null, message: null, }; } }; export const resetPlugin = async (): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + "/sfsync/v1/settings", { method: "DELETE", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, }, ); let data = null; try { data = await response.json(); } catch (error: any) { return { error: true, status: response.status, message: null }; } if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const refreshPlan = async (): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + "/sfsync/v1/refresh-plan", { method: "POST", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, }, ); let data = null; try { data = await response.json(); } catch (error: any) { return { error: true, status: response.status, message: null }; } if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const saveConfig = async ( instagramId: string, payload: SaveConfigPayload, ): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + `/sfsync/v1/config/${instagramId}`, { method: "POST", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify(payload), }, ); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const syncPosts = async (payload: SyncPostsPayload): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + "/sfsync/v1/posts/sync", { method: "POST", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify(payload), }, ); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const linkPost = async ( postId: string, payload: LinkPostPayload, ): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + `/sfsync/v1/posts/${postId}/link`, { method: "POST", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify(payload), }, ); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const unlinkPost = async ( postId: string, payload: UnlinkPostPayload, ): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + `/sfsync/v1/posts/${postId}/link`, { method: "DELETE", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify(payload), }, ); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const pinPost = async ( postId: string, payload: PinPostPayload, ): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + `/sfsync/v1/posts/${postId}/pin`, { method: "POST", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify(payload), }, ); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const unpinPost = async ( postId: string, payload: UnpinPostPayload, ): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + `/sfsync/v1/posts/${postId}/pin`, { method: "DELETE", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify(payload), }, ); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const configSave = async ( apiKey: string, payload: ConfigSavePayload ): Promise => { try { const response = await fetch(baseURL + "/config/save", { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey, "plugin-version": sfsyncRestAPI.pluginVersion, }, body: JSON.stringify({ ...payload, }), }); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data.message, }; } return data; } catch (error: any) { return { error: true, status: null, message: null, }; } }; export const saveApiKey = async ( apiKey: string ): Promise => { try { const payload: SaveApiKeyPayload = { api_key: apiKey }; const response = await fetch( sfsyncRestAPI.baseURL + "/sfsync/v1/save-api-key", { method: "POST", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify(payload), } ); let data = null; try { data = await response.json(); } catch (error: any) { return { error: true, status: response.status, message: null }; } if (!response.ok) { return { error: true, status: response.status, message: data?.message }; } return data; } catch (error: any) { return { error: true, status: null, message: null }; } }; export const postsRefresh = async ( apiKey: string, payload: PostsRefreshPayload, ifUpdatedSince: string = "" ): Promise => { try { const response = await fetch(baseURL + "/posts/refresh", { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey, "if-modified-since": ifUpdatedSince, "plugin-version": sfsyncRestAPI.pluginVersion, }, body: JSON.stringify({ ...payload, }), }); if (response.status === 304) { return { status: 304, posts: [], lastModified: "", }; } const lastModified = response.headers.get("last-modified"); const responseJSON = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: responseJSON.message, }; } return { posts: responseJSON.data as Post[], lastModified: lastModified ? lastModified : "", }; } catch (error: any) { return { error: true, status: null, message: null, }; } }; export const searchWPPosts = async ( searchTerm: string, lang: string, currentScope: string ): Promise => { if (searchTerm) { let subtype = currentScope; let query = `?subtype=${subtype}&search=${searchTerm}&_embed`; if (lang) { query += `&lang=${lang}`; } try { const response = await fetch( sfsyncRestAPI.baseURL + `/wp/v2/search/${query}` ); const responseJSON = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: responseJSON.message, }; } else { //formatting the response let searchResults = responseJSON.map((ele: any) => ({ id: ele.id, title: decode(ele.title), url: ele.url, pubDate: ele["_embedded"].self[0].date, type: ele.subtype, })); return searchResults; } } catch (error) { return { error: true, status: null, message: null, }; } } else { return []; } }; export const postsManage = async ( apiKey: string, payload: PostsManagePayload ): Promise => { try { const response = await fetch(baseURL + "/posts/manage", { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey, "plugin-version": sfsyncRestAPI.pluginVersion, }, body: JSON.stringify({ ...payload, }), }); const responseJSON = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: responseJSON.message, }; } return responseJSON; } catch (error: any) { return { error: true, status: null, message: null, }; } }; export const getDataFromURL = async ( payload: GetDataFromURLPayload ): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + "/sfsync/v1/get-data-from-url", { method: "POST", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, body: JSON.stringify({ ...payload, }), } ); if (!response.ok) { const data = await response.json(); return { error: true, status: response.status, message: data.message, }; } else { const data = await response.json(); return data; } } catch (error: any) { return { error: true, status: null, message: null, }; } }; export const getHomeURLs = async (): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + "/sfsync/v1/get-home-urls", { method: "GET", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, } ); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data.message, }; } else { return data; } } catch (error: any) { return { error: true, status: null, message: null, }; } }; export const getSiteStatus = async (): Promise => { try { const response = await fetch( sfsyncRestAPI.baseURL + "/sfsync/v1/site-status", { method: "GET", headers: { "Content-Type": "application/json", "X-WP-Nonce": sfsyncRestAPI.nonceValue, }, } ); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data.message, }; } else { return data; } } catch (error: any) { return { error: true, status: null, message: null, }; } }; export const verifyAPIKey = async (apiKey: string): Promise => { try { const response = await fetch(baseURL + "/scf/validateKey", { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey, "plugin-version": sfsyncRestAPI.pluginVersion, }, body: JSON.stringify({}), }); const data = await response.json(); if (!response.ok) { return { error: true, status: response.status, message: data.message, }; } return data; } catch (error: any) { return { error: true, status: null, message: null, }; } };