@pinegrow/headless-wordpress
Version:
Pinegrow Headless Wordpress Plugin
1 lines • 7.79 kB
JavaScript
!function(e,n){if("object"==typeof exports&&"object"==typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{var s=n();for(var t in s)("object"==typeof exports?exports:e)[t]=s[t]}}(global,(()=>(()=>{"use strict";var e={d:(n,s)=>{for(var t in s)e.o(s,t)&&!e.o(n,t)&&Object.defineProperty(n,t,{enumerable:!0,get:s[t]})},o:(e,n)=>Object.prototype.hasOwnProperty.call(e,n),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},n={};e.r(n),e.d(n,{default:()=>o});const s=require("path");var t=function(e){return e&&e[0].toUpperCase()+e.slice(1)||""},r={plugin_path:"./plugin.cjs",templates_path:"./templates",version:"6.7.0",major_version:"6",minor_version:"7",info_badge:"",type:"wordpress",short_type:"wp",allow_single_type:!0,has_actions:!0,show_in_action_tab:"DATA",short_name:"Headless Wordpress",dependency:"@pinegrow/headless-wordpress",default:!0,get name(){return"".concat(this.short_name," ").concat(this.major_version,".").concat(this.minor_version)},get key(){return"".concat(this.type)},get scriptTagId(){return"".concat(this.short_type)},get prefix(){return"".concat(this.short_type).concat(this.major_version,".").concat(this.minor_version)},get dataPrefix(){return"pg".concat(t(this.type))},get dataName(){return"Pg".concat(t(this.type))},syncWithLibrarySemVer:"minor",get description(){return'<a href="https://developer.wordpress.org/rest-api/">'.concat(this.name,"</a> components. Some samples are from ").concat(this.name," documentation and based on ").concat(this.short_name," ").concat(this.version,".")},author:"Pinegrow",get author_link(){return"http://github.com/pinegrow/".concat(this.type,"-plugin")},get show_in_manager(){return!1},skip_opening_new_pages_created_from_templates:!0,show_templates_only_inside_projects:!0,get simple(){return this.pgf_type&&this.pgf_type.simple||!1},ignore_css_files:[/(^|\/)wordpress\.(css)/i,/(^|\/)wordpress\.min\.(css)/i,/(^|\/)materialdesignicons\.(css)/i,/(^|\/)materialdesignicons\.min\.(css)/i],detect_async:function(e,n){e.stylesheetsThatContain(/wordpress\sv3/i,/wordpress(\.min|)\.(css)/i,(function(e){n(e.length>0)}))}};const o={name:r.name,key:r.key,library:r.library,syncWithLibrarySemVer:r.syncWithLibrarySemVer,pluginPath:s.resolve(__dirname,r.plugin_path),dependency:r.dependency,dependencyRoot:s.resolve(__dirname,".."),options:{},dataTemplate:"/* Auto-generated logic by Vue Designer ".concat(r.name," begins */\n/* eslint-disable */\n// @ts-nocheck\n\nimport { $fetch } from 'ofetch'\nimport type {\n WP_REST_API_Posts,\n WP_REST_API_Post,\n WP_REST_API_Categories,\n WP_REST_API_Category,\n WP_REST_API_Tags,\n WP_REST_API_Tag,\n WP_REST_API_Comments,\n WP_REST_API_Comment,\n WP_REST_API_Attachments,\n WP_REST_API_Attachment,\n WP_REST_API_Users,\n WP_REST_API_User,\n} from 'wp-types'\n\nconst wordpressUrl: string = import.meta.env.VITE_WORDPRESS_URL\n\ninterface FetchArgs {\n filters?: Record<string, string | number | string[]>\n fields?: string[]\n}\n\nconst fetchWordpressData = async <T>({\n filters = {},\n fields = [],\n endpoint,\n endpointIsACollection = true,\n}: FetchArgs & { endpoint: string }): Promise<T | null> => {\n const filtersArgs = Object.entries(filters).reduce((acc, [filter, value]) => {\n const filterRecord = `${filter}=${value}`\n return [...acc, filterRecord]\n }, [] as string[])\n\n const fieldsParam = []\n const fieldsVal = fields.join(',')\n if (fieldsVal) {\n fieldsParam.push(`_fields=${fieldsVal}`)\n }\n\n const args = [...filtersArgs, ...fieldsParam].join('&')\n\n let response\n\n if (endpointIsACollection) {\n response = await $fetch<T>(\n `${wordpressUrl}/wp-json/wp/v2/${endpoint}${args ? `?${args}` : ''}`,\n )\n } else {\n if (filters.id) {\n response = await $fetch<T>(\n `${wordpressUrl}/wp-json/wp/v2/${endpoint}/${filters.id}${filters._embed ? `?_embed=${filters._embed}` : ''}`,\n )\n } else {\n response = null // No data is returned, caller should use v-if to ensure this is handled\n }\n }\n return response\n}\n\nconst getPgWordpressPosts = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Posts> => {\n return fetchWordpressData<WP_REST_API_Posts>({ ...args, endpoint: 'posts' })\n}\n\nconst getPgWordpressPost = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Post> => {\n return fetchWordpressData<WP_REST_API_Post>({\n ...args,\n endpoint: 'posts',\n endpointIsACollection: false,\n })\n}\n\nconst getPgWordpressCategories = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Categories> => {\n return fetchWordpressData<WP_REST_API_Categories>({\n ...args,\n endpoint: 'categories',\n })\n}\n\nconst getPgWordpressCategory = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Category> => {\n return fetchWordpressData<WP_REST_API_Category>({\n ...args,\n endpoint: 'categories',\n endpointIsACollection: false,\n })\n}\n\nconst getPgWordpressTags = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Tags> => {\n return fetchWordpressData<WP_REST_API_Tags>({\n ...args,\n endpoint: 'tags',\n })\n}\n\nconst getPgWordpressTag = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Tag> => {\n return fetchWordpressData<WP_REST_API_Tag>({\n ...args,\n endpoint: 'tags',\n endpointIsACollection: false,\n })\n}\n\nconst getPgWordpressComments = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Comments> => {\n return fetchWordpressData<WP_REST_API_Comments>({\n ...args,\n endpoint: 'comments',\n })\n}\n\nconst getPgWordpressComment = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Comment> => {\n return fetchWordpressData<WP_REST_API_Comment>({\n ...args,\n endpoint: 'comments',\n endpointIsACollection: false,\n })\n}\n\nconst getPgWordpressAttachments = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Attachments> => {\n return fetchWordpressData<WP_REST_API_Attachments>({\n ...args,\n endpoint: 'media',\n })\n}\n\nconst getPgWordpressAttachment = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Attachment> => {\n return fetchWordpressData<WP_REST_API_Attachment>({\n ...args,\n endpoint: 'media',\n endpointIsACollection: false,\n })\n}\n\nconst getPgWordpressUsers = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_Users> => {\n return fetchWordpressData<WP_REST_API_Users>({\n ...args,\n endpoint: 'users',\n })\n}\n\nconst getPgWordpressUser = async (\n args: FetchArgs = {},\n): Promise<WP_REST_API_User> => {\n return fetchWordpressData<WP_REST_API_User>({\n ...args,\n endpoint: 'users',\n endpointIsACollection: false,\n })\n}\n\ninterface WP_SITE_INFO {\n name: string\n description: string\n url: string\n home: string\n}\n\nconst getPgWordpressSiteinfo = async ({}): Promise<WP_SITE_INFO> => {\n const fields = ['name', 'description', 'url', 'home']\n const args = fields.join('&')\n const siteInfo = await $fetch(\n `${wordpressUrl}/wp-json${args ? `?${args}` : ''}`,\n )\n return siteInfo\n}\n\nconst usePgWordpressData = () => {\n return {\n getPgWordpressSiteinfo,\n getPgWordpressPosts,\n getPgWordpressPost,\n getPgWordpressCategories,\n getPgWordpressCategory,\n getPgWordpressTags,\n getPgWordpressTag,\n getPgWordpressComments,\n getPgWordpressComment,\n getPgWordpressAttachments,\n getPgWordpressAttachment,\n getPgWordpressUsers,\n getPgWordpressUser,\n }\n}\n\nexport { usePgWordpressData }\n/* Auto-generated logic by Vue Designer ").concat(r.name," ends */"),init:function(){return{templatesPath:r.templates_path}}};return n})()));