|
| 1 | +# Migration Guide to v0.30.0 |
| 2 | + |
| 3 | +## Breaking Changes |
| 4 | + |
| 5 | +### REST API Changes |
| 6 | + |
| 7 | +The REST API has been completely redesigned to improve type safety, reduce dependencies, and better align with web standards. The main changes are: |
| 8 | + |
| 9 | +1. Removal of `option-t` dependency |
| 10 | + - All `Result` types from `option-t/plain_result` have been replaced with `ScrapboxResponse` |
| 11 | + - No more `unwrapOk`, `isErr`, or other option-t utilities |
| 12 | + |
| 13 | +2. New `ScrapboxResponse` class |
| 14 | + - Extends the web standard `Response` class |
| 15 | + - Direct access to `body`, `headers`, and other standard Response properties |
| 16 | + - Type-safe error handling based on HTTP status codes |
| 17 | + - Built-in JSON parsing with proper typing for success/error cases |
| 18 | + |
| 19 | +### Before and After Examples |
| 20 | + |
| 21 | +#### Before (v0.29.x): |
| 22 | +```typescript |
| 23 | +import { isErr, unwrapOk } from "option-t/plain_result"; |
| 24 | + |
| 25 | +const result = await getProfile(); |
| 26 | +if (isErr(result)) { |
| 27 | + console.error("Failed:", result); |
| 28 | + return; |
| 29 | +} |
| 30 | +const profile = unwrapOk(result); |
| 31 | +console.log("Name:", profile.name); |
| 32 | +``` |
| 33 | + |
| 34 | +#### After (v0.30.0): |
| 35 | +```typescript |
| 36 | +const response = await getProfile(); |
| 37 | +if (!response.ok) { |
| 38 | + console.error("Failed:", response.error); |
| 39 | + return; |
| 40 | +} |
| 41 | +console.log("Name:", response.data.name); |
| 42 | +``` |
| 43 | + |
| 44 | +### Key Benefits |
| 45 | + |
| 46 | +1. **Simpler Error Handling** |
| 47 | + - HTTP status codes determine error types |
| 48 | + - No need to unwrap results manually |
| 49 | + - Type-safe error objects with proper typing |
| 50 | + |
| 51 | +2. **Web Standard Compatibility** |
| 52 | + - Works with standard web APIs without conversion |
| 53 | + - Direct access to Response properties |
| 54 | + - Compatible with standard fetch patterns |
| 55 | + |
| 56 | +3. **Better Type Safety** |
| 57 | + - Response types change based on HTTP status |
| 58 | + - Proper typing for both success and error cases |
| 59 | + - No runtime overhead for type checking |
| 60 | + |
| 61 | +### Migration Steps |
| 62 | + |
| 63 | +1. Replace `option-t` imports: |
| 64 | + ```diff |
| 65 | + - import { isErr, unwrapOk } from "option-t/plain_result"; |
| 66 | + ``` |
| 67 | + |
| 68 | +2. Update error checking: |
| 69 | + ```diff |
| 70 | + - if (isErr(result)) { |
| 71 | + - console.error(result); |
| 72 | + + if (!response.ok) { |
| 73 | + + console.error(response.error); |
| 74 | + ``` |
| 75 | + |
| 76 | +3. Access response data: |
| 77 | + ```diff |
| 78 | + - const data = unwrapOk(result); |
| 79 | + + const data = response.data; |
| 80 | + ``` |
| 81 | + |
| 82 | +4. For direct Response access: |
| 83 | + ```typescript |
| 84 | + // Access headers |
| 85 | + const contentType = response.headers.get("content-type"); |
| 86 | + |
| 87 | + // Access raw body |
| 88 | + const text = await response.text(); |
| 89 | + |
| 90 | + // Parse JSON with type safety |
| 91 | + const json = await response.json(); |
| 92 | + ``` |
| 93 | + |
| 94 | +### Common Patterns |
| 95 | + |
| 96 | +1. **Status-based Error Handling**: |
| 97 | +```typescript |
| 98 | +const response = await getSnapshot(project, pageId, timestampId); |
| 99 | + |
| 100 | +if (response.status === 422) { |
| 101 | + // Handle invalid snapshot ID |
| 102 | + console.error("Invalid snapshot:", response.error); |
| 103 | + return; |
| 104 | +} |
| 105 | + |
| 106 | +if (!response.ok) { |
| 107 | + // Handle other errors |
| 108 | + console.error("Failed:", response.error); |
| 109 | + return; |
| 110 | +} |
| 111 | + |
| 112 | +// Use the data |
| 113 | +console.log(response.data); |
| 114 | +``` |
| 115 | + |
| 116 | +2. **Type-safe JSON Parsing**: |
| 117 | +```typescript |
| 118 | +const response = await getTweetInfo(tweetUrl); |
| 119 | +if (response.ok) { |
| 120 | + const tweet = response.data; // Properly typed as TweetInfo |
| 121 | + console.log(tweet.text); |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +3. **Working with Headers**: |
| 126 | +```typescript |
| 127 | +const response = await uploadToGCS(file, projectId); |
| 128 | +if (!response.ok && response.headers.get("Content-Type")?.includes("/xml")) { |
| 129 | + console.error("GCS Error:", await response.text()); |
| 130 | + return; |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### Need Help? |
| 135 | + |
| 136 | +If you encounter any issues during migration, please: |
| 137 | +1. Check the examples in this guide |
| 138 | +2. Review the [API documentation](https://jsr.io/@takker/scrapbox-userscript-std) |
| 139 | +3. Open an issue on GitHub if you need further assistance |
0 commit comments