Skip to content

Commit

Permalink
Second iteration ;)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkruglikov committed Jan 2, 2025
1 parent ba561f9 commit 7e1ab56
Show file tree
Hide file tree
Showing 45 changed files with 1,408 additions and 232 deletions.
502 changes: 500 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"packageManager": "npm@8.5.0",
"scripts": {
"prepare": "npm run build",
"prepare": "turbo build",
"check-types": "tsc --noEmit",
"build": "turbo check-types build",
"dev": "turbo dev"
Expand All @@ -23,9 +23,12 @@
"devDependencies": {
"@types/chrome": "^0.0.287",
"@types/node": "^22.10.2",
"copy-webpack-plugin": "^12.0.2",
"css-loader": "^7.1.2",
"microbundle": "^0.15.1",
"msw": "^2.7.0",
"prettier": "^3.4.2",
"style-loader": "^4.0.0",
"ts-loader": "^9.5.1",
"turbo": "^2.3.3",
"typescript": "^5.7.2",
Expand Down
7 changes: 7 additions & 0 deletions packages/connect/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { WindowWithMswDevtools } from '@msw-devtools/core'

declare global {
interface Window extends WindowWithMswDevtools {}
}

export {}
2 changes: 1 addition & 1 deletion packages/connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"license": "MIT",
"types": "./dist/index.d.ts",
"source": "src/index.ts",
"source": "./src/index.ts",
"exports": {
"require": "./dist/index.cjs",
"default": "./dist/index.modern.js"
Expand Down
7 changes: 0 additions & 7 deletions packages/connect/src/constants.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/connect/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './resolver'
export * from './constants'
53 changes: 13 additions & 40 deletions packages/connect/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
import { HttpResponse, passthrough, ResponseResolver } from 'msw'

import { MESSAGE_TYPE } from './constants'
import { passthrough, ResponseResolver, HttpResponse } from 'msw'

export const responseResolver: ResponseResolver = ({ requestId, request }) => {
return new Promise((resolve, reject) => {
window.postMessage(
{
type: MESSAGE_TYPE.injected,
requestId,
request: {
id: requestId,
method: request.method,
url: request.url
}
},
window.location.origin
)
const handleMessage = (
e: MessageEvent<{
type: (typeof MESSAGE_TYPE)[keyof typeof MESSAGE_TYPE]
requestId: string
response: {
body: BodyInit
init?: ResponseInit
}
}>
) => {
if (
e.data.requestId !== requestId ||
e.data.type === MESSAGE_TYPE.injected
) {
return
}

window.removeEventListener('message', handleMessage)

if (e.data.type === MESSAGE_TYPE.unhandledRequest) {
resolve(passthrough())
} else if (e.data.type === MESSAGE_TYPE.handledRequest) {
resolve(new HttpResponse(e.data.response.body, e.data.response.init))
}
if (window.__MSW_DEVTOOLS_EXTENSION?.resolve) {
window.__MSW_DEVTOOLS_EXTENSION
.resolve({ requestId, request })
.then((response) => {
resolve(new HttpResponse(response.body, response.init))
})
.catch((e) => {
console.log(e)
resolve(passthrough())
})
} else {
resolve(passthrough())
}
window.addEventListener('message', handleMessage)
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/connect/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"]
"include": ["./src/**/*", "./global.d.ts"]
}
16 changes: 16 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@msw-devtools/core",
"version": "0.1",
"type": "module",
"license": "MIT",
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"main": "./dist/index.js",
"files": [
"dist"
],
"scripts": {
"build": "microbundle --no-pkg-main -f modern",
"dev": "microbundle watch --no-pkg-main -f modern"
}
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types'
68 changes: 68 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { JsonConfig } from '@msw-devtools/json-config'
import type { ResponseResolver } from 'msw'

export enum MessageType {
Content = 'msw-devtools-extension-content',
HandledRequest = 'msw-devtools-extension_handled',
SetJsonConfig = 'msw-devtools-extension_setJsonConfig',
UnhandledRequest = 'msw-devtools-extension_unhandled',
Status = 'msw-devtools-extension_status',
Injected = 'msw-devtools-extension-injected'
}

export type MswDevtoolsExtensionTransporter = {
resolve: (...args: Parameters<ResponseResolver>) => Promise<any>
}

export interface WindowWithMswDevtools {
__MSW_DEVTOOLS_EXTENSION?: MswDevtoolsExtensionTransporter
}

export type ChromeExtensionLocalStorage = {
jsonConfig: JsonConfig
}

export type BackgroundReceiveMessage =
| {
type: MessageType.Content
request: {
url: string
method: string
}
}
| {
type: MessageType.SetJsonConfig
payload: JsonConfig
}
| {
type: MessageType.Status
}

export type BackgroundResponseMessage =
| {
type: MessageType.HandledRequest
response: JsonConfig[string]
}
| {
type: MessageType.UnhandledRequest
}
| {
type: MessageType.SetJsonConfig
status: 'success'
}
| ({
type: MessageType.Status
} & (
| { status: 'error' }
| {
status: 'success'
payload: {
host: string
hasHandle: boolean
hasConfig: boolean
configIsValid: boolean
}
}
))

export type { JsonConfig }
4 changes: 4 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["./src/**/*"]
}
5 changes: 3 additions & 2 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
"build": "NODE_ENV=production npx webpack"
},
"devDependencies": {
"@msw-devtools/connect": "*"
"@msw-devtools/connect": "*",
"@msw-devtools/json-config": "*"
},
"msw": {
"workerDirectory": [
"public"
"dist"
]
}
}
13 changes: 0 additions & 13 deletions packages/demo/src/json-config.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/demo/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const createWebpackConfig = require('../../webpack.common.js')

module.exports = {
...createWebpackConfig({ root: __dirname }),
...createWebpackConfig({ root: __dirname, port: 8081 }),
entry: {
main: './src/index.ts'
}
Expand Down
4 changes: 4 additions & 0 deletions packages/extension/declaration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.css' {
const content: Record<string, string>
export default content
}
7 changes: 7 additions & 0 deletions packages/extension/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { WindowWithMswDevtools } from '@msw-devtools/core'

declare global {
interface Window extends WindowWithMswDevtools {}
}

export {}
8 changes: 7 additions & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"build": "NODE_ENV=production npx webpack"
},
"devDependencies": {
"@msw-devtools/connect": "*"
"@msw-devtools/connect": "*",
"@msw-devtools/json-config": "*",
"@types/react": "^19.0.2",
"@types/react-dom": "^19.0.2",
"clsx": "^2.1.1",
"react": "^19.0.0",
"react-dom": "^19.0.0"
}
}
Binary file added packages/extension/public/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions packages/extension/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
"run_at": "document_start"
}
],
"web_accessible_resources": [
{
"resources": ["injected.js"],
"matches": ["<all_urls>"]
}
],
"permissions": ["storage", "tabs", "activeTab"],
"action": {
"default_icon": "icon.png",
"default_popup": "popup.html",
"default_title": "Upload JSON"
}
Expand Down
13 changes: 2 additions & 11 deletions packages/extension/public/popup.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<!doctype html>
<html lang="en">
<head>
<title>Upload JSON</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 0;
margin: 0;
}
</style>
<title>MSW devtools extension</title>
</head>
<body>
<input type="file" id="fileInput" accept=".json" />
<button id="uploadButton">Upload</button>
<p id="status"></p>
<div id="root"></div>
<script type="module" src="popup.js"></script>
</body>
</html>
Loading

0 comments on commit 7e1ab56

Please sign in to comment.