UNPKG

@wordpress/e2e-test-utils-playwright

Version:
8 lines (7 loc) 3.05 kB
{ "version": 3, "sources": ["../../src/request-utils/media.ts"], "sourcesContent": ["/**\n * External dependencies\n */\nimport * as fs from 'fs';\n\n/**\n * Internal dependencies\n */\nimport type { RequestUtils } from './index';\n\nexport interface Media {\n\tid: number;\n\ttitle: {\n\t\traw: string;\n\t\trendered: string;\n\t};\n\tsource_url: string;\n\tslug: string;\n\talt_text: string;\n\tcaption: { rendered: string };\n\tlink: string;\n}\n\n/**\n * List all media files.\n *\n * @see https://developer.wordpress.org/rest-api/reference/media/#list-media\n * @param this\n */\nasync function listMedia( this: RequestUtils ) {\n\tconst response = await this.rest< Media[] >( {\n\t\tmethod: 'GET',\n\t\tpath: '/wp/v2/media',\n\t\tparams: {\n\t\t\tper_page: 100,\n\t\t},\n\t} );\n\n\treturn response;\n}\n\n/**\n * Upload a media file.\n *\n * @see https://developer.wordpress.org/rest-api/reference/media/#create-a-media-item\n * @param this\n * @param filePathOrData The path or data of the file being uploaded.\n */\nasync function uploadMedia(\n\tthis: RequestUtils,\n\tfilePathOrData: string | fs.ReadStream\n) {\n\tconst file =\n\t\ttypeof filePathOrData === 'string'\n\t\t\t? fs.createReadStream( filePathOrData )\n\t\t\t: filePathOrData;\n\n\tconst response = await this.rest< Media >( {\n\t\tmethod: 'POST',\n\t\tpath: '/wp/v2/media',\n\t\tmultipart: {\n\t\t\tfile,\n\t\t},\n\t} );\n\n\treturn response;\n}\n\n/**\n * delete a media file.\n *\n * @see https://developer.wordpress.org/rest-api/reference/media/#delete-a-media-item\n * @param this\n * @param mediaId The ID of the media file.\n */\nasync function deleteMedia( this: RequestUtils, mediaId: number ) {\n\tconst response = await this.rest( {\n\t\tmethod: 'DELETE',\n\t\tpath: `/wp/v2/media/${ mediaId }`,\n\t\tparams: { force: true },\n\t} );\n\n\treturn response;\n}\n\n/**\n * delete all media files.\n *\n * @param this\n */\nasync function deleteAllMedia( this: RequestUtils ) {\n\tconst files = await this.listMedia();\n\n\t// The media endpoint doesn't support batch request yet.\n\tconst responses = await Promise.all(\n\t\tfiles.map( ( media ) => this.deleteMedia( media.id ) )\n\t);\n\n\treturn responses;\n}\n\nexport { listMedia, uploadMedia, deleteMedia, deleteAllMedia };\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,SAAoB;AA0BpB,eAAe,YAAgC;AAC9C,QAAM,WAAW,MAAM,KAAK,KAAiB;AAAA,IAC5C,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,QAAQ;AAAA,MACP,UAAU;AAAA,IACX;AAAA,EACD,CAAE;AAEF,SAAO;AACR;AASA,eAAe,YAEd,gBACC;AACD,QAAM,OACL,OAAO,mBAAmB,WACpB,oBAAkB,cAAe,IACpC;AAEJ,QAAM,WAAW,MAAM,KAAK,KAAe;AAAA,IAC1C,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,WAAW;AAAA,MACV;AAAA,IACD;AAAA,EACD,CAAE;AAEF,SAAO;AACR;AASA,eAAe,YAAiC,SAAkB;AACjE,QAAM,WAAW,MAAM,KAAK,KAAM;AAAA,IACjC,QAAQ;AAAA,IACR,MAAM,gBAAiB,OAAQ;AAAA,IAC/B,QAAQ,EAAE,OAAO,KAAK;AAAA,EACvB,CAAE;AAEF,SAAO;AACR;AAOA,eAAe,iBAAqC;AACnD,QAAM,QAAQ,MAAM,KAAK,UAAU;AAGnC,QAAM,YAAY,MAAM,QAAQ;AAAA,IAC/B,MAAM,IAAK,CAAE,UAAW,KAAK,YAAa,MAAM,EAAG,CAAE;AAAA,EACtD;AAEA,SAAO;AACR;", "names": [] }