@@ -72,8 +72,8 @@ import {
7272 PutPageFileNamePropertiesByKeyParams ,
7373 PutPageFileNamePropertiesByKeyResponse ,
7474 PutPageImportParams ,
75- PutPageMoveParams ,
76- PutPageMoveResponse ,
75+ PostPageMoveParams ,
76+ PostPageMoveResponse ,
7777 PutPageOrderParams ,
7878 PostPagePropertiesParams ,
7979 PostPagePropertiesResponse ,
@@ -95,6 +95,7 @@ import {
9595 PutPageTagsResponse
9696} from "../types" ;
9797import BaseModule from "./base" ;
98+ import { ExpertError } from "../errors" ;
9899import { Buffer } from 'buffer' ;
99100
100101export default class Pages extends BaseModule {
@@ -819,23 +820,44 @@ export default class Pages extends BaseModule {
819820 return res . data ;
820821 }
821822
823+ /**
824+ * Copies a page to a new location. `to` is required by the endpoint and takes
825+ * a plain path - the double encoding Deki requires is applied here.
826+ * @param id - A page ID, page path, or the literal `"home"`.
827+ * @param reqArgs - Copy destination and options.
828+ * @param funcArgs - Optional per-call auth/tld/headers overrides.
829+ * @returns The copied pages.
830+ * @throws {ExpertError } With `kind: 'config'` when `to` is missing or blank.
831+ */
822832 public async postPageCopied (
823833 id : string | number ,
824- reqArgs ? : PostCopyPageParams ,
834+ reqArgs : PostCopyPageParams ,
825835 funcArgs ?: BaseArgs
826836 ) {
827- this . debug ( 'postPageCopied called for page:' , id ) ;
837+ this . debug ( 'postPageCopied called for page:' , id , 'to:' , reqArgs ?. to ) ;
838+
839+ // `to` is the only required param, and JavaScript callers can skip
840+ // `reqArgs` entirely, so check it before anything dereferences it.
841+ if ( typeof reqArgs ?. to !== 'string' || reqArgs . to . trim ( ) === '' ) {
842+ throw ExpertError . config (
843+ 'Invalid copy request: `to` must be a non-empty destination path.'
844+ ) ;
845+ }
846+
828847 const pageId = this . parsePageId ( id ) ;
829848 const requests = this . prepare ( funcArgs ) ;
830849
850+ // Deki wants the destination path double-encoded; axios supplies the
851+ // second layer when it serializes the query string.
852+ const params = {
853+ ...reqArgs ,
854+ to : this . parsePathParam ( reqArgs . to ) ,
855+ } ;
856+
831857 const res = await requests . post < PostCopyPageResponse > (
832858 `/pages/${ pageId } /copy` ,
833859 "" ,
834- {
835- params : {
836- ...reqArgs ,
837- } ,
838- }
860+ { params }
839861 ) ;
840862
841863 this . debug ( 'postPageCopied successfully copied page:' , id ) ;
@@ -1044,29 +1066,94 @@ export default class Pages extends BaseModule {
10441066 return res . data ;
10451067 }
10461068
1047- public async putPageMove (
1069+ /**
1070+ * Moves a page, and every page beneath it, to a new location. Deki leaves a
1071+ * redirect alias behind at the old path.
1072+ *
1073+ * Pass `to` for a full relocation, or `parentid`/`name`/`title` to reparent
1074+ * or rename the page where it sits. `to` takes a plain path - the double
1075+ * encoding Deki requires is applied here.
1076+ * @param id - A page ID, page path, or the literal `"home"`.
1077+ * @param reqArgs - Move target and options. At least one of `to`, `parentid`,
1078+ * `name`, or `title` must be supplied. `parentid` accepts a number or a
1079+ * string of digits.
1080+ * @param funcArgs - Optional per-call auth/tld/headers overrides.
1081+ * @returns The moved pages.
1082+ * @throws {ExpertError } With `kind: 'config'` when no move target is supplied,
1083+ * when `to`/`name`/`title` are blank, or when `parentid` is not a positive
1084+ * integer page ID.
1085+ */
1086+ public async postPageMove (
10481087 id : string | number ,
1049- reqArgs ?: PutPageMoveParams ,
1088+ reqArgs ?: PostPageMoveParams ,
10501089 funcArgs ?: BaseArgs
10511090 ) {
1052- this . debug ( 'putPageMove called for page:' , id , 'to:' , reqArgs ?. to ) ;
1091+ this . debug ( 'postPageMove called for page:' , id , 'to:' , reqArgs ?. to ) ;
1092+
1093+ // Every move target is individually optional, but a move with none of them
1094+ // is a no-op the API rejects, so catch it before the request goes out.
1095+ // A supplied-but-junk target (empty string, zero, a fractional id) is the
1096+ // same rejection, so validate the values rather than their presence.
1097+ const isNonBlankString = ( value : unknown ) =>
1098+ typeof value === 'string' && value . trim ( ) !== '' ;
1099+
1100+ for ( const key of [ 'to' , 'name' , 'title' ] as const ) {
1101+ if ( reqArgs ?. [ key ] !== undefined && ! isNonBlankString ( reqArgs [ key ] ) ) {
1102+ throw ExpertError . config (
1103+ `Invalid move request: \`${ key } \` must be a non-empty string.`
1104+ ) ;
1105+ }
1106+ }
1107+
1108+ // `parentid` follows the same rules as any other page ID: digit strings
1109+ // are accepted and coerced, everything else throws.
1110+ const parentid =
1111+ reqArgs ?. parentid !== undefined
1112+ ? this . parseNumericId ( reqArgs . parentid , 'parent page' )
1113+ : undefined ;
1114+
1115+ const hasTarget = ( [ 'to' , 'parentid' , 'name' , 'title' ] as const ) . some (
1116+ ( key ) => reqArgs ?. [ key ] !== undefined
1117+ ) ;
1118+ if ( ! hasTarget ) {
1119+ throw ExpertError . config (
1120+ 'Invalid move request: supply at least one of `to`, `parentid`, `name`, or `title`.'
1121+ ) ;
1122+ }
1123+
10531124 const pageId = this . parsePageId ( id ) ;
10541125 const requests = this . prepare ( funcArgs ) ;
10551126
1056- const res = await requests . put < PutPageMoveResponse > (
1127+ // Deki wants the destination path double-encoded; axios supplies the
1128+ // second layer when it serializes the query string.
1129+ const params = {
1130+ ...reqArgs ,
1131+ ...( reqArgs ?. to !== undefined && { to : this . parsePathParam ( reqArgs . to ) } ) ,
1132+ ...( parentid !== undefined && { parentid } ) ,
1133+ } ;
1134+
1135+ const res = await requests . post < PostPageMoveResponse > (
10571136 `/pages/${ pageId } /move` ,
10581137 "" ,
1059- {
1060- params : {
1061- ...reqArgs ,
1062- } ,
1063- }
1138+ { params }
10641139 ) ;
10651140
1066- this . debug ( 'putPageMove successfully moved page:' , id ) ;
1141+ this . debug ( 'postPageMove successfully moved page:' , id ) ;
10671142 return res . data ;
10681143 }
10691144
1145+ /**
1146+ * @deprecated Use {@link Pages.postPageMove}. The move endpoint is a POST,
1147+ * not a PUT; this alias forwards to the corrected implementation.
1148+ */
1149+ public async putPageMove (
1150+ id : string | number ,
1151+ reqArgs ?: PostPageMoveParams ,
1152+ funcArgs ?: BaseArgs
1153+ ) {
1154+ return this . postPageMove ( id , reqArgs , funcArgs ) ;
1155+ }
1156+
10701157 public async putPageOrder (
10711158 id : string | number ,
10721159 reqArgs ?: PutPageOrderParams ,
0 commit comments