11import { readFileSync } from 'node:fs' ;
22import { resolve } from 'path' ;
3- import { PathParams , RestRequest , rest } from 'msw' ;
3+ import { http , HttpResponse } from 'msw' ;
44
55import { MsgSetMswInterceptedDataFromApi } from '../../bin/frameworks/messages/messages' ;
66
@@ -12,111 +12,109 @@ const FIGMA_API = 'https://api.figma.com/v1';
1212const FIGMA_BUCKET_URL =
1313 'https://s3-us-west-2.amazonaws.com/figma-alpha-api/img' ;
1414
15- const logInterceptedRequest = ( req : RestRequest < any , PathParams > ) =>
15+ const logInterceptedRequest = ( req : any ) =>
1616 console . log ( MsgSetMswInterceptedDataFromApi ( req . url . href ) ) ;
1717
1818export const handlers = [
19- rest . get ( `${ FIGMA_API } /files/:file` , ( req , res , ctx ) => {
20- logInterceptedRequest ( req ) ;
19+ http . get ( `${ FIGMA_API } /files/:file` , ( { request , params } ) => {
20+ logInterceptedRequest ( request ) ;
2121 // force a response error if we get invalid-url
22- if ( req . params . file === 'invalid-url' ) {
23- return res (
24- ctx . status ( 403 ) ,
25- ctx . json ( {
22+ if ( params . file === 'invalid-url' ) {
23+ return HttpResponse . json (
24+ {
2625 status : 403 ,
2726 err : 'Not allowed' ,
28- } ) ,
27+ } ,
28+ {
29+ status : 403 ,
30+ } ,
2931 ) ;
3032 }
3133
32- return res ( ctx . status ( 200 ) , ctx . json ( mockFigmaFiles ) ) ;
34+ return HttpResponse . json ( mockFigmaFiles , { status : 200 } ) ;
3335 } ) ,
34- rest . get ( `${ FIGMA_API } /files/:file/versions` , ( req , res , ctx ) => {
35- logInterceptedRequest ( req ) ;
36- return res ( ctx . status ( 200 ) , ctx . json ( mockFigmaFilesVersions ) ) ;
36+ http . get ( `${ FIGMA_API } /files/:file/versions` , ( { request } ) => {
37+ logInterceptedRequest ( request ) ;
38+ return HttpResponse . json ( mockFigmaFilesVersions , { status : 200 } ) ;
3739 } ) ,
38- rest . get ( `${ FIGMA_API } /images/:file` , ( req , res , ctx ) => {
39- logInterceptedRequest ( req ) ;
40- return res ( ctx . status ( 200 ) , ctx . json ( mockFigmaImages ) ) ;
40+ http . get ( `${ FIGMA_API } /images/:file` , ( { request } ) => {
41+ logInterceptedRequest ( request ) ;
42+ return HttpResponse . json ( mockFigmaImages , { status : 200 } ) ;
4143 } ) ,
42- rest . get (
44+ http . get (
4345 `${ FIGMA_BUCKET_URL } /2f0d/1106/be444c1f7c5a79484c415e50e9010847` ,
44- ( req , res , ctx ) => {
45- logInterceptedRequest ( req ) ;
46- return res (
47- ctx . status ( 200 ) ,
48- ctx . text (
49- `<svg width="55" height="56" viewBox="0 0 55 56" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M2.11131 28.0662L17.0333 51.499L52.1723 1.43805" stroke="#219653" stroke-width="5"/></svg>` ,
50- ) ,
46+ ( { request } ) => {
47+ logInterceptedRequest ( request ) ;
48+ return HttpResponse . text (
49+ `<svg width="55" height="56" viewBox="0 0 55 56" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M2.11131 28.0662L17.0333 51.499L52.1723 1.43805" stroke="#219653" stroke-width="5"/></svg>` ,
50+ { status : 200 } ,
5151 ) ;
5252 } ,
5353 ) ,
54- rest . get (
54+ http . get (
5555 `${ FIGMA_BUCKET_URL } /8ba5/2322/bac5a41a1a59a9a76a12bbe21e0cd781` ,
56- ( req , res , ctx ) => {
57- logInterceptedRequest ( req ) ;
58- return res (
59- ctx . status ( 200 ) ,
60- ctx . text (
61- `<svg width="50" height="50" viewBox="0 0 50 50" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="16" cy="25" r="3" fill="black"/><circle cx="25" cy="25" r="3" fill="black"/><circle cx="34" cy="25" r="3" fill="black"/><circle cx="25" cy="25" r="23.5" stroke="#333333" stroke-width="3"/></svg>` ,
62- ) ,
56+ ( { request } ) => {
57+ logInterceptedRequest ( request ) ;
58+ return HttpResponse . text (
59+ `<svg width="50" height="50" viewBox="0 0 50 50" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="16" cy="25" r="3" fill="black"/><circle cx="25" cy="25" r="3" fill="black"/><circle cx="34" cy="25" r="3" fill="black"/><circle cx="25" cy="25" r="23.5" stroke="#333333" stroke-width="3"/></svg>` ,
60+ { status : 200 } ,
6361 ) ;
6462 } ,
6563 ) ,
66- rest . get (
64+ http . get (
6765 `${ FIGMA_BUCKET_URL } /3e15/3c93/1e96386d904b25bf987abc4b87c62ee1` ,
68- ( req , res , ctx ) => {
69- logInterceptedRequest ( req ) ;
70- return res (
71- ctx . status ( 200 ) ,
72- ctx . text (
73- `<svg width="54" height="54" viewBox="0 0 54 54" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1.78311 1.7831L52.2169 52.2169M1.78311 52.2169L52.2169 1.7831" stroke="black" stroke-width="5"/></svg>` ,
74- ) ,
66+ ( { request } ) => {
67+ logInterceptedRequest ( request ) ;
68+ return HttpResponse . json (
69+ `<svg width="54" height="54" viewBox="0 0 54 54" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1.78311 1.7831L52.2169 52.2169M1.78311 52.2169L52.2169 1.7831" stroke="black" stroke-width="5"/></svg>` ,
70+ { status : 200 } ,
7571 ) ;
7672 } ,
7773 ) ,
78- rest . get (
74+ http . get (
7975 'https://localhost/v1/files/lkhjtkl34kljf-fg3kj3443.hjt3hjk.net/.kj34jkl34' ,
80- ( req , res ) => {
81- logInterceptedRequest ( req ) ;
82- return res . networkError ( 'Connection Refused' ) ;
76+ ( { request } ) => {
77+ logInterceptedRequest ( request ) ;
78+ return HttpResponse . error ( ) ;
8379 } ,
8480 ) ,
85- rest . get (
81+ http . get (
8682 'https://s3-us-west-2.amazonaws.com/figma-alpha-api/img/0882/9cc7/c4731c8d0df07592cd6f7dc0519bb3bb-asdf' ,
87- ( req , res , ctx ) => {
88- logInterceptedRequest ( req ) ;
89- return res ( ctx . status ( 403 ) ) ;
83+ ( { request } ) => {
84+ logInterceptedRequest ( request ) ;
85+ return HttpResponse . text ( null , { status : 403 } ) ;
9086 } ,
9187 ) ,
92- rest . get (
88+ http . get (
9389 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png' ,
94- ( req , res , ctx ) => {
95- logInterceptedRequest ( req ) ;
90+ ( { request } ) => {
91+ logInterceptedRequest ( request ) ;
9692
9793 const imageBuffer = readFileSync (
9894 resolve ( __dirname , './files/googlelogo_color_272x92dp.png' ) ,
9995 ) ;
100- return res (
101- ctx . set ( 'Content-Length' , imageBuffer . byteLength . toString ( ) ) ,
102- ctx . set ( 'Content-Type' , 'image/jpeg' ) ,
103- ctx . body ( imageBuffer ) ,
104- ) ;
96+ return HttpResponse . arrayBuffer ( imageBuffer as any , {
97+ headers : {
98+ 'Content-Length' : imageBuffer . byteLength . toString ( ) ,
99+ 'Content-Type' : 'image/jpeg' ,
100+ } ,
101+ } ) ;
105102 } ,
106103 ) ,
107- rest . get (
104+ http . get (
108105 'https://upload.wikimedia.org/wikipedia/commons/5/53/Google_%22G%22_Logo.svg' ,
109- ( req , res , ctx ) => {
110- logInterceptedRequest ( req ) ;
106+ ( { request } ) => {
107+ logInterceptedRequest ( request ) ;
111108
112109 const imageBuffer = readFileSync (
113110 resolve ( __dirname , './files/Google__G__Logo.svg' ) ,
114111 ) ;
115- return res (
116- ctx . set ( 'Content-Length' , imageBuffer . byteLength . toString ( ) ) ,
117- ctx . set ( 'Content-Type' , 'image/jpeg' ) ,
118- ctx . body ( imageBuffer ) ,
119- ) ;
112+ return HttpResponse . arrayBuffer ( imageBuffer as any , {
113+ headers : {
114+ 'Content-Length' : imageBuffer . byteLength . toString ( ) ,
115+ 'Content-Type' : 'image/jpeg' ,
116+ } ,
117+ } ) ;
120118 } ,
121119 ) ,
122120] ;
0 commit comments