1
+ import { z } from 'zod' ;
2
+ import { zod } from '../../../../utils/zod.js' ;
1
3
import { Logger } from '../../../../cli/Logger.js' ;
2
- import GlobalOptions from '../../../../GlobalOptions .js' ;
3
- import request from '../../../../request.js' ;
4
+ import { globalOptionsZod } from '../../../../Command .js' ;
5
+ import request , { CliRequestOptions } from '../../../../request.js' ;
4
6
import { formatting } from '../../../../utils/formatting.js' ;
5
7
import { urlUtil } from '../../../../utils/urlUtil.js' ;
6
8
import { validation } from '../../../../utils/validation.js' ;
7
9
import SpoCommand from '../../../base/SpoCommand.js' ;
8
10
import commands from '../../commands.js' ;
9
11
12
+ const options = globalOptionsZod
13
+ . extend ( {
14
+ webUrl : zod . alias ( 'u' , z . string ( )
15
+ . refine ( url => validation . isValidSharePointUrl ( url ) === true , url => ( {
16
+ message : `'${ url } ' is not a valid SharePoint Online site URL.`
17
+ } ) )
18
+ ) ,
19
+ name : zod . alias ( 'n' , z . string ( ) ) . optional ( ) ,
20
+ default : z . boolean ( ) . optional ( ) ,
21
+ metadataOnly : z . boolean ( ) . optional ( )
22
+ } )
23
+ . strict ( ) ;
24
+ declare type Options = z . infer < typeof options > ;
25
+
10
26
interface CommandArgs {
11
27
options : Options ;
12
28
}
13
29
14
- interface Options extends GlobalOptions {
15
- name : string ;
16
- webUrl : string ;
17
- metadataOnly ?: boolean ;
18
- }
19
-
20
30
class SpoPageGetCommand extends SpoCommand {
21
31
public get name ( ) : string {
22
32
return commands . PAGE_GET ;
@@ -30,45 +40,43 @@ class SpoPageGetCommand extends SpoCommand {
30
40
return [ 'commentsDisabled' , 'numSections' , 'numControls' , 'title' , 'layoutType' ] ;
31
41
}
32
42
33
- constructor ( ) {
34
- super ( ) ;
35
-
36
- this . #initOptions( ) ;
37
- this . #initValidators( ) ;
43
+ public get schema ( ) : z . ZodTypeAny {
44
+ return options ;
38
45
}
39
46
40
- #initOptions( ) : void {
41
- this . options . unshift (
42
- {
43
- option : '-n, --name <name>'
44
- } ,
45
- {
46
- option : '-u, --webUrl <webUrl>'
47
- } ,
48
- {
49
- option : '--metadataOnly'
50
- }
51
- ) ;
52
- }
53
-
54
- #initValidators( ) : void {
55
- this . validators . push (
56
- async ( args : CommandArgs ) => validation . isValidSharePointUrl ( args . options . webUrl )
57
- ) ;
47
+ public getRefinedSchema ( schema : typeof options ) : z . ZodEffects < any > | undefined {
48
+ return schema
49
+ . refine ( options => [ options . name , options . default ] . filter ( x => x !== undefined ) . length === 1 , {
50
+ message : `Specify either name or default, but not both.`
51
+ } ) ;
58
52
}
59
53
60
54
public async commandAction ( logger : Logger , args : CommandArgs ) : Promise < void > {
61
55
if ( this . verbose ) {
62
56
await logger . logToStderr ( `Retrieving information about the page...` ) ;
63
57
}
64
58
65
- let pageName : string = args . options . name ;
66
- if ( args . options . name . indexOf ( '.aspx' ) < 0 ) {
67
- pageName += '.aspx' ;
68
- }
69
-
59
+ let pageName : string = '' ;
70
60
try {
71
- let requestOptions : any = {
61
+ if ( args . options . name ) {
62
+ pageName = args . options . name . endsWith ( '.aspx' )
63
+ ? args . options . name
64
+ : `${ args . options . name } .aspx` ;
65
+ }
66
+ else if ( args . options . default ) {
67
+ const requestOptions : CliRequestOptions = {
68
+ url : `${ args . options . webUrl } /_api/Web/RootFolder?$select=WelcomePage` ,
69
+ headers : {
70
+ accept : 'application/json;odata=nometadata'
71
+ } ,
72
+ responseType : 'json'
73
+ } ;
74
+
75
+ const { WelcomePage } = await request . get < { WelcomePage : string } > ( requestOptions ) ;
76
+ pageName = WelcomePage . split ( '/' ) . pop ( ) ! ;
77
+ }
78
+
79
+ let requestOptions : CliRequestOptions = {
72
80
url : `${ args . options . webUrl } /_api/web/GetFileByServerRelativePath(DecodedUrl='${ urlUtil . getServerRelativeSiteUrl ( args . options . webUrl ) } /SitePages/${ formatting . encodeQueryParameter ( pageName ) } ')?$expand=ListItemAllFields/ClientSideApplicationId,ListItemAllFields/PageLayoutType,ListItemAllFields/CommentsDisabled` ,
73
81
headers : {
74
82
'content-type' : 'application/json;charset=utf-8' ,
@@ -80,7 +88,7 @@ class SpoPageGetCommand extends SpoCommand {
80
88
const page = await request . get < any > ( requestOptions ) ;
81
89
82
90
if ( page . ListItemAllFields . ClientSideApplicationId !== 'b6917cb1-93a0-4b97-a84d-7cf49975d4ec' ) {
83
- throw `Page ${ args . options . name } is not a modern page.` ;
91
+ throw `Page ${ pageName } is not a modern page.` ;
84
92
}
85
93
86
94
let pageItemData : any = { } ;
0 commit comments