From 1d4c4cb631372359236616f9254b6b9783bc4558 Mon Sep 17 00:00:00 2001 From: Otavio Cordeiro Date: Tue, 2 Jan 2024 17:46:39 +0100 Subject: [PATCH] Publish new pages using Micropub JSON --- src/networking/NetworkRequestFactory.ts | 26 ++++++++++---------- src/networking/NewPageRequest.ts | 32 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 src/networking/NewPageRequest.ts diff --git a/src/networking/NetworkRequestFactory.ts b/src/networking/NetworkRequestFactory.ts index c74ea3c..28615c6 100644 --- a/src/networking/NetworkRequestFactory.ts +++ b/src/networking/NetworkRequestFactory.ts @@ -1,6 +1,7 @@ import { NetworkRequest } from '@networking/NetworkRequest' -import { makeUpdatePostRequest } from '@networking/UpdatePostRequest' +import { makeNewPageRequest } from '@networking/NewPageRequest' import { makeNewPostRequest } from '@networking/NewPostRequest' +import { makeUpdatePostRequest } from '@networking/UpdatePostRequest' export interface NetworkRequestFactoryInterface { @@ -83,22 +84,19 @@ export class NetworkRequestFactory implements NetworkRequestFactoryInterface { blogID: string, navigation: boolean ): NetworkRequest { - const parameters = new URLSearchParams([ - ['h', 'entry'], - ['name', title], - ['content', content], - ['mp-channel', 'pages'], - ['mp-navigation', navigation.toString()] - ]) - - if (blogID.length > 0 && blogID != 'default') { - parameters.append('mp-destination', blogID) - } + const body = JSON.stringify( + makeNewPageRequest( + title, + content, + blogID, + navigation + ) + ) return { path: '/micropub', - parameters: parameters, - method: 'POST' + method: 'POST', + body: body } } diff --git a/src/networking/NewPageRequest.ts b/src/networking/NewPageRequest.ts new file mode 100644 index 0000000..378c65a --- /dev/null +++ b/src/networking/NewPageRequest.ts @@ -0,0 +1,32 @@ +/* + * Definition of the JSON used to create a page. + */ +export type NewPageRequest = { + 'type': string + 'mp-destination'?: string + 'mp-channel': string + 'mp-navigation': string + 'properties': { + 'name': string[] + 'content': string[], + } +} + +// Factory method to create the `NewPageRequest`. +export function makeNewPageRequest( + title: string, + content: string, + blogID: string, + navigation: boolean +): NewPageRequest { + return { + 'type': "h-entry", + ...blogID.length > 0 && blogID !== 'default' && { 'mp-destination': blogID }, + 'mp-channel': 'pages', + 'mp-navigation': navigation.toString(), + 'properties': { + 'name': [title], + 'content': [content] + } + } +}