|
| 1 | +# Migration Guide |
| 2 | + |
| 3 | +## From version 3 to 4 |
| 4 | + |
| 5 | +#### resourceSaver option |
| 6 | + |
| 7 | +Create plugin class which adds `saveResource` action |
| 8 | +```javascript |
| 9 | +// before |
| 10 | +scrape({ |
| 11 | + resourceSaver: class MyResourceSaver { |
| 12 | + saveResource (resource) {/* code to save file where you need */} |
| 13 | + errorCleanup (err) {/* code to remove all previously saved files in case of error */} |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +// after |
| 18 | +class CustomSaveResourcePlugin { |
| 19 | + apply(registerAction) { |
| 20 | + registerAction('saveResource', ({resource}) => {/* code to save file where you need */}) |
| 21 | + } |
| 22 | +} |
| 23 | +scrape({ |
| 24 | + plugins: [ new CustomSaveResourcePlugin() ] |
| 25 | +}) |
| 26 | +``` |
| 27 | + |
| 28 | +#### updateSources option |
| 29 | + |
| 30 | +Create plugin class which adds `getReference` action |
| 31 | +```javascript |
| 32 | +// before |
| 33 | +scrape({ |
| 34 | + updateSources: false |
| 35 | +}) |
| 36 | + |
| 37 | +// after |
| 38 | +class MyGetReferencePlugin { |
| 39 | + apply(registerAction) { |
| 40 | + registerAction('getReference', () => ({ reference: null })) |
| 41 | + } |
| 42 | +} |
| 43 | +scrape({ |
| 44 | + plugins: [ new MyGetReferencePlugin() ] |
| 45 | +}) |
| 46 | +``` |
| 47 | + |
| 48 | +#### updateMissingSources option |
| 49 | + |
| 50 | +Create plugin class which adds `getReference` action |
| 51 | +```javascript |
| 52 | +// before |
| 53 | +scrape({ |
| 54 | + updateMissingSources: true |
| 55 | +}) |
| 56 | + |
| 57 | +// after |
| 58 | +class MyGetReferencePlugin { |
| 59 | + apply(registerAction) { |
| 60 | + registerAction('getReference', ({resource, parentResource, originalReference}) => { |
| 61 | + if (!resource) { |
| 62 | + return { reference: getAbsoluteUrl(parentResource, originalReference) } |
| 63 | + } |
| 64 | + return getRelativePath(parentResource.getFilename(), resource.getFilename()); |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | +scrape({ |
| 69 | + plugins: [ new MyGetReferencePlugin() ] |
| 70 | +}) |
| 71 | +``` |
| 72 | + |
| 73 | +#### filenameGenerator option |
| 74 | + |
| 75 | +For functions only, if you use string `byType` or `byStructure` - you don't need to do anything. |
| 76 | + |
| 77 | +Create plugin class which adds `generateFilename` action |
| 78 | +```javascript |
| 79 | +// before |
| 80 | +scrape({ |
| 81 | + filenameGenerator: (resource, options, occupiedFileNames) => { |
| 82 | + return crypto.randomBytes(20).toString('hex'); |
| 83 | + } |
| 84 | +}) |
| 85 | + |
| 86 | +// after |
| 87 | +class MyGenerateFilenamePlugin { |
| 88 | + apply(registerAction) { |
| 89 | + registerAction('generateFilename', ({resource}) => { |
| 90 | + return {filename: crypto.randomBytes(20).toString('hex')}; |
| 91 | + }); |
| 92 | + } |
| 93 | +} |
| 94 | +scrape({ |
| 95 | + plugins: [ new MyGenerateFilenamePlugin() ] |
| 96 | +}) |
| 97 | +``` |
| 98 | + |
| 99 | +#### httpResponseHandler option |
| 100 | + |
| 101 | +Create plugin class which adds `afterResponse` action |
| 102 | +```javascript |
| 103 | +// before |
| 104 | +scrape({ |
| 105 | + httpResponseHandler: (response) => { |
| 106 | + if (response.statusCode === 404) { |
| 107 | + return Promise.reject(new Error('status is 404')); |
| 108 | + } else { |
| 109 | + return Promise.resolve(response.body); |
| 110 | + } |
| 111 | +}) |
| 112 | + |
| 113 | +// after |
| 114 | +class MyAfterResponsePlugin { |
| 115 | + apply(registerAction) { |
| 116 | + registerAction('afterResponse', ({response}) => { |
| 117 | + if (response.statusCode === 404) { |
| 118 | + return Promise.reject(new Error('status is 404')); |
| 119 | + } else { |
| 120 | + return Promise.resolve(response.body); |
| 121 | + }); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
| 126 | +scrape({ |
| 127 | + plugins: [ new MyAfterResponsePlugin() ] |
| 128 | +}) |
| 129 | +``` |
| 130 | +
|
| 131 | +#### request option |
| 132 | +For functions only, if you use static request object - you don't need to do anything. |
| 133 | +
|
| 134 | +Create plugin class which adds `beforeRequest` action |
| 135 | +```javascript |
| 136 | +// before |
| 137 | +scrape({ |
| 138 | + request: resource => ({qs: {myParam: 123}}) |
| 139 | +}) |
| 140 | + |
| 141 | +// after |
| 142 | +class MyBeforeRequestPlugin { |
| 143 | + apply(registerAction) { |
| 144 | + registerAction('beforeRequest', ({resource, requestOptions}) => { |
| 145 | + return {requestOptions: {qs: {myParam: 123}}}; |
| 146 | + }); |
| 147 | + } |
| 148 | +} |
| 149 | +scrape({ |
| 150 | + plugins: [ new MyBeforeRequestPlugin() ] |
| 151 | +}) |
| 152 | +``` |
| 153 | +
|
| 154 | +#### onResourceSaved and onResourceError options |
| 155 | +
|
| 156 | +Create plugin class which adds `onResourceSaved` and `onResourceError` actions |
| 157 | +```javascript |
| 158 | +// before |
| 159 | +scrape({ |
| 160 | + onResourceSaved: (resource) => { |
| 161 | + console.log(`Resource ${resource} was saved to fs`); |
| 162 | + }, |
| 163 | + onResourceError: (resource, err) => { |
| 164 | + console.log(`Resource ${resource} was not saved because of ${err}`); |
| 165 | + } |
| 166 | +}) |
| 167 | + |
| 168 | +// after |
| 169 | +class MyPlugin { |
| 170 | + apply(registerAction) { |
| 171 | + registerAction('onResourceSaved', ({resource}) => console.log(`Resource ${resource.url} saved!`)); |
| 172 | + registerAction('onResourceError', ({resource, error}) => console.log(`Resource ${resource.url} has error ${error}`)); |
| 173 | + } |
| 174 | +} |
| 175 | +scrape({ |
| 176 | + plugins: [ new MyPlugin() ] |
| 177 | +}) |
| 178 | +``` |
0 commit comments