Skip to content

Commit

Permalink
fix(endpoint-micropub): add debug logs (remove from PR)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdbd committed Jun 6, 2024
1 parent 9373f71 commit b4ab29f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/endpoint-micropub/lib/controllers/action.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import makeDebug from "debug";
import { IndiekitError } from "@indiekit/error";
import { formEncodedToJf2, mf2ToJf2 } from "../jf2.js";
import { postContent } from "../post-content.js";
import { postData } from "../post-data.js";
import { checkScope } from "../scope.js";
import { uploadMedia } from "../media.js";

const debug = makeDebug(`indiekit:endpoint-micropub:controllers:action`);

/**
* Perform requested post action
* @type {import("express").RequestHandler}
Expand Down Expand Up @@ -41,6 +44,7 @@ export const actionController = async (request, response, next) => {
let content;
switch (action) {
case "create": {
debug(`create and normalise JF2 data %O`, body);
// Create and normalise JF2 data
jf2 = request.is("json")
? await mf2ToJf2(body, publication.enrichPostData)
Expand All @@ -57,6 +61,7 @@ export const actionController = async (request, response, next) => {
}

case "update": {
debug(`update URL ${url} with body %O`, body);
// Check for update operations
if (!(body.replace || body.add || body.remove)) {
throw IndiekitError.badRequest(
Expand Down Expand Up @@ -94,12 +99,14 @@ export const actionController = async (request, response, next) => {
}

case "delete": {
debug(`delete URL ${url}`);
data = await postData.delete(application, publication, url);
content = await postContent.delete(publication, data);
break;
}

case "undelete": {
debug(`undelete URL ${url}`);
data = await postData.undelete(
application,
publication,
Expand Down
2 changes: 2 additions & 0 deletions packages/endpoint-micropub/lib/post-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ export const postData = {
const { posts } = application;
const query = { "properties.url": url };

debug(`try finding MongoDB document matching query %O`, query);
const postData = await posts.findOne(query);
if (!postData) {
throw IndiekitError.notFound(url);
}
debug(`found MongoDB document matching query %O`, postData);

return postData;
},
Expand Down
7 changes: 7 additions & 0 deletions packages/endpoint-micropub/lib/update.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import makeDebug from "debug";
import _ from "lodash";
import { mf2ToJf2 } from "./jf2.js";

const debug = makeDebug("indiekit:endpoint-micropub:update");

/**
* Add properties to object
* @param {object} object - Object to update
* @param {object} additions - Properties to add (mf2)
* @returns {object|undefined} Updated object
*/
export const addProperties = (object, additions) => {
debug(`addProperties %O`, { object, additions });
for (const key in additions) {
if (Object.prototype.hasOwnProperty.call(additions, key)) {
const newValue = additions[key];
Expand Down Expand Up @@ -45,6 +49,7 @@ export const addProperties = (object, additions) => {
* @returns {Promise<object>} Updated object (JF2)
*/
export const replaceEntries = async (object, replacements) => {
debug(`replaceEntries %O`, { object, replacements });
for await (const [key, value] of Object.entries(replacements)) {
if (!Array.isArray(value)) {
throw new TypeError("Replacement value should be an array");
Expand Down Expand Up @@ -82,6 +87,7 @@ export const replaceEntries = async (object, replacements) => {
* @returns {object} Updated object
*/
export const deleteEntries = (object, deletions) => {
debug(`replaceEntries %O`, { object, deletions });
for (const key in deletions) {
if (Object.prototype.hasOwnProperty.call(deletions, key)) {
const valuesToDelete = deletions[key];
Expand Down Expand Up @@ -120,6 +126,7 @@ export const deleteEntries = (object, deletions) => {
* @returns {object} Updated object
*/
export const deleteProperties = (object, deletions) => {
debug(`deleteProperties %O`, { object, deletions });
for (const key of deletions) {
delete object[key];
}
Expand Down

0 comments on commit b4ab29f

Please sign in to comment.