Skip to content

v0.11.0-beta1

Pre-release
Pre-release
Compare
Choose a tag to compare
@icebob icebob released this 04 Dec 21:29
· 2 commits to master since this release

Full Changelog: v0.10.7...v0.11.0-beta1

Changes

Updated path-ro-regexp library

The path-to-regexp has been updated to 8.x.x. It contains many breaking changes in the path resolving. Check the documentation of library to how migrate your alias paths.

Optional parameter alias path

    // Old way
    "GET user/:name?": "user.get"

    // New way
    "GET user{/:name}": "user.get"

Repeating parameter alias path

    // Old way
    "GET /users/*username": "user.resolveUsersByNames",

    // New way
    "GET /users/:username*": "user.resolveUsersByNames",

Using 0.15 new streaming solution

The [email protected] supports Moleculer v0.15.x including the new streaming solution. It means, it doesn't support 0.13 and 0.14 moleculer versions.

Thanks for the new solution, the multipart fields and request parameters are sent via ctx.params instead of meta and the file stream is available in ctx.stream in action handlers.

Example

module.exports = {
    name: "file",
    actions: {
        save: {
            handler(ctx) {
                return new this.Promise((resolve, reject) => {
                    const filePath = path.join(uploadDir, ctx.params.$filename);
                    const f = fs.createWriteStream(filePath);
                    f.on("close", () => {
                        // File written successfully
                        this.logger.info(`Uploaded file stored in '${filePath}'`);
                        resolve({ filePath });
                    });

                    ctx.stream.on("error", err => {
                        this.logger.info("File error received", err.message);
                        reject(err);

                        // Destroy the local file
                        f.destroy(err);
                    });

                    f.on("error", () => {
                        // Remove the errored file.
                        fs.unlinkSync(filePath);
                    });

                    ctx.stream.pipe(f);
                });
            }
        }
    }
};

Example content of ctx.params:

{
    // Multipart file properties
    $fieldname: "myfile", 
    $filename: "avatar.png",
    $encoding: "7bit",
    $mimetype: "image/png",
    
    // Other multipart fields
    // e.g.: `<input type="text" name="name" id="name" value="Test User">`
    name: "Test User", 

    // Request path parameter, e.g.: `/upload/single/1234`
    id: "1234" 
}