[{"_path":"/blog/dart-shelf-exceptions-adapter","_draft":false,"_partial":false,"_empty":false,"title":"Properly handling exceptions with Dart Shelf","description":"How to properly handle Exceptions by creating a custom adapter for Shelf library in Dart.","excerpt":{"type":"root","children":[{"type":"element","tag":"h2","props":{"id":"introduction"},"children":[{"type":"text","value":"Introduction"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Handling exceptions properly is necessary when writing any code to keep things running smoothly, give proper feedback if something didn't work as expected, and numerous other reasons. "},{"type":"element","tag":"a","props":{"href":"https://pub.dev/packages/shelf","rel":["nofollow","noopener","noreferrer"],"target":"_blank","title":"Shelf"},"children":[{"type":"text","value":"Shelf"}]},{"type":"text","value":" is a fantastic web server package made by Google but it's difficult to to cleanly handle exceptions without lots of boilerplate code that is prone to errors, time consuming to write, and less maintainable. A good way to fix this problem is to make a custom adapter to use with Shelf."}]},{"type":"element","tag":"h2","props":{"id":"what-is-an-adapter"},"children":[{"type":"text","value":"What is an Adapter?"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"According to the "},{"type":"element","tag":"a","props":{"href":"https://pub.dev/documentation/shelf/latest/","rel":["nofollow","noopener","noreferrer"],"target":"_blank","title":"API reference"},"children":[{"type":"text","value":"API reference"}]},{"type":"text","value":" an adapter is \"any code that creates "},{"type":"element","tag":"a","props":{"href":"https://pub.dev/documentation/shelf/latest/shelf/Request-class.html","rel":["nofollow","noopener","noreferrer"],"target":"_blank"},"children":[{"type":"text","value":"Request"}]},{"type":"text","value":" objects, passes them to a handler, and deals with the resulting "},{"type":"element","tag":"a","props":{"href":"https://pub.dev/documentation/shelf/latest/shelf/Response-class.html","rel":["nofollow","noopener","noreferrer"],"target":"_blank"},"children":[{"type":"text","value":"Response"}]},{"type":"text","value":".\""}]},{"type":"element","tag":"h2","props":{"id":"preparations"},"children":[{"type":"text","value":"Preparations"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"For most efficient use of Exceptions adapter, we need to properly use Exception throws. First of all make sure that you have properly implemented all exceptions that will be thrown."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tag":"code","props":{"code":"class MyException extends Exception {\n    final String message;\n    MyException(this.message);\n}\n","language":"dart"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"class MyException extends Exception {\n    final String message;\n    MyException(this.message);\n}\n"}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Make few exceptions for your use case. Generally you should have at least one exception for each main error that will be thrown. (e.g. NotAuthorisedException, BadRequestException, etc.)"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"We will throw them and then we will use Exceptions adapter to handle them."}]},{"type":"element","tag":"code","props":{"code":"void somefunc(UserModel? user) {\n    if (user == null) {\n        throw NotAuthorisedException(\"User is not authorised\");\n    }\n}\n","language":"dart"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"void somefunc(UserModel? user) {\n    if (user == null) {\n        throw NotAuthorisedException(\"User is not authorised\");\n    }\n}\n"}]}]}]},{"type":"element","tag":"h2","props":{"id":"how-to-init-the-exceptions-adapter"},"children":[{"type":"text","value":"How to init the Exceptions adapter"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"In desired folder create a file called "},{"type":"element","tag":"code-inline","props":{},"children":[{"type":"text","value":"exception_adapter.dart"}]},{"type":"text","value":".\nDon't forget to add export to "},{"type":"element","tag":"code-inline","props":{},"children":[{"type":"text","value":"exports.dart"}]},{"type":"text","value":"."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"This will be a file that will contain all Exceptions adapter code."}]},{"type":"element","tag":"code","props":{"code":"class ExceptionsAdapter {\n  final Handler _handler; // this is the next handler in the chain\n\n  ExceptionsAdapter(this._handler); \n\n\n/// It will be called for every request, \n/// and it will be responsible for handling exceptions above\n  Handler get handler => (Request request) async {\n        try {\n          return await _handler(request);\n        } on BadRequestException catch (e) {\n          return Response.badRequest(body: e.message);,\n          );\n        } on NotAuthorisedException catch (e) {\n          return Response.(\n            401 // Unauthorized http status code\n            body: e.message);\n        } \n        /// you should add all other exceptions here that you want to handle\n\n        /// Don't forget to add general catch for all other exceptions\n        catch (e) {\n          return Response.serverError(body: \"Internal server error\"); \n          // for safery reasons we shouldn't return unknown exception to user\n        }\n    };\n}\n","language":"dart"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"class ExceptionsAdapter {\n  final Handler _handler; // this is the next handler in the chain\n\n  ExceptionsAdapter(this._handler); \n\n\n/// It will be called for every request, \n/// and it will be responsible for handling exceptions above\n  Handler get handler => (Request request) async {\n        try {\n          return await _handler(request);\n        } on BadRequestException catch (e) {\n          return Response.badRequest(body: e.message);,\n          );\n        } on NotAuthorisedException catch (e) {\n          return Response.(\n            401 // Unauthorized http status code\n            body: e.message);\n        } \n        /// you should add all other exceptions here that you want to handle\n\n        /// Don't forget to add general catch for all other exceptions\n        catch (e) {\n          return Response.serverError(body: \"Internal server error\"); \n          // for safery reasons we shouldn't return unknown exception to user\n        }\n    };\n}\n"}]}]}]},{"type":"element","tag":"h2","props":{"id":"how-to-use-exceptions-adapter"},"children":[{"type":"text","value":"How to use Exceptions adapter"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Now we have created adapter but it now needs to be used in the pipeline."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"In a file with your server pipeline you need to add the following lines:"}]},{"type":"element","tag":"code","props":{"code":"  // this is your main handler that will be called for every request\n  final handler = MainController().handler; \n  // this is handler that will handle all exceptions\n  // it takes the next handler in the chain as an argument\n  final exceptionAdapterHandler = ExceptionsAdapter(handler).handler; \n\n  final _handler = Pipeline()\n      .addMiddleware(logRequests())\n      .addHandler(exceptionAdapterHandler);\n\n  final server = await serve(_handler, ip, port);\n","language":"dart"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"  // this is your main handler that will be called for every request\n  final handler = MainController().handler; \n  // this is handler that will handle all exceptions\n  // it takes the next handler in the chain as an argument\n  final exceptionAdapterHandler = ExceptionsAdapter(handler).handler; \n\n  final _handler = Pipeline()\n      .addMiddleware(logRequests())\n      .addHandler(exceptionAdapterHandler);\n\n  final server = await serve(_handler, ip, port);\n"}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Our request will follow the following pipeline:"}]},{"type":"element","tag":"ol","props":{},"children":[{"type":"element","tag":"li","props":{},"children":[{"type":"text","value":"log middleware"}]},{"type":"element","tag":"li","props":{},"children":[{"type":"text","value":"exception adapter middleware"}]},{"type":"element","tag":"li","props":{},"children":[{"type":"text","value":"main controller handler"}]}]}]},"mid":2,"lang":"en","tags":["Dart","Back-end"],"ttr":15,"image":"https://i.imgur.com/E62R1IG.png","date":"2022-07-10T21:00:00.000Z","body":{"type":"root","children":[{"type":"element","tag":"h2","props":{"id":"introduction"},"children":[{"type":"text","value":"Introduction"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Handling exceptions properly is necessary when writing any code to keep things running smoothly, give proper feedback if something didn't work as expected, and numerous other reasons. "},{"type":"element","tag":"a","props":{"href":"https://pub.dev/packages/shelf","rel":["nofollow","noopener","noreferrer"],"target":"_blank","title":"Shelf"},"children":[{"type":"text","value":"Shelf"}]},{"type":"text","value":" is a fantastic web server package made by Google but it's difficult to to cleanly handle exceptions without lots of boilerplate code that is prone to errors, time consuming to write, and less maintainable. A good way to fix this problem is to make a custom adapter to use with Shelf."}]},{"type":"element","tag":"h2","props":{"id":"what-is-an-adapter"},"children":[{"type":"text","value":"What is an Adapter?"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"According to the "},{"type":"element","tag":"a","props":{"href":"https://pub.dev/documentation/shelf/latest/","rel":["nofollow","noopener","noreferrer"],"target":"_blank","title":"API reference"},"children":[{"type":"text","value":"API reference"}]},{"type":"text","value":" an adapter is \"any code that creates "},{"type":"element","tag":"a","props":{"href":"https://pub.dev/documentation/shelf/latest/shelf/Request-class.html","rel":["nofollow","noopener","noreferrer"],"target":"_blank"},"children":[{"type":"text","value":"Request"}]},{"type":"text","value":" objects, passes them to a handler, and deals with the resulting "},{"type":"element","tag":"a","props":{"href":"https://pub.dev/documentation/shelf/latest/shelf/Response-class.html","rel":["nofollow","noopener","noreferrer"],"target":"_blank"},"children":[{"type":"text","value":"Response"}]},{"type":"text","value":".\""}]},{"type":"element","tag":"h2","props":{"id":"preparations"},"children":[{"type":"text","value":"Preparations"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"For most efficient use of Exceptions adapter, we need to properly use Exception throws. First of all make sure that you have properly implemented all exceptions that will be thrown."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"For example:"}]},{"type":"element","tag":"code","props":{"code":"class MyException extends Exception {\n    final String message;\n    MyException(this.message);\n}\n","language":"dart"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"class"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"MyException"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"extends"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Exception"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"final"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"String"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" message;"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"MyException"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"this"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":".message);"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"}"}]}]}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Make few exceptions for your use case. Generally you should have at least one exception for each main error that will be thrown. (e.g. NotAuthorisedException, BadRequestException, etc.)"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"We will throw them and then we will use Exceptions adapter to handle them."}]},{"type":"element","tag":"code","props":{"code":"void somefunc(UserModel? user) {\n    if (user == null) {\n        throw NotAuthorisedException(\"User is not authorised\");\n    }\n}\n","language":"dart"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"void"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"somefunc"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"UserModel"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"? user) {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"if"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" (user == "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"null"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":") {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"throw"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"NotAuthorisedException"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"\"User is not authorised\""}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":");"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    }"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"}"}]}]}]}]}]},{"type":"element","tag":"h2","props":{"id":"how-to-init-the-exceptions-adapter"},"children":[{"type":"text","value":"How to init the Exceptions adapter"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"In desired folder create a file called "},{"type":"element","tag":"code-inline","props":{},"children":[{"type":"text","value":"exception_adapter.dart"}]},{"type":"text","value":".\nDon't forget to add export to "},{"type":"element","tag":"code-inline","props":{},"children":[{"type":"text","value":"exports.dart"}]},{"type":"text","value":"."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"This will be a file that will contain all Exceptions adapter code."}]},{"type":"element","tag":"code","props":{"code":"class ExceptionsAdapter {\n  final Handler _handler; // this is the next handler in the chain\n\n  ExceptionsAdapter(this._handler); \n\n\n/// It will be called for every request, \n/// and it will be responsible for handling exceptions above\n  Handler get handler => (Request request) async {\n        try {\n          return await _handler(request);\n        } on BadRequestException catch (e) {\n          return Response.badRequest(body: e.message);,\n          );\n        } on NotAuthorisedException catch (e) {\n          return Response.(\n            401 // Unauthorized http status code\n            body: e.message);\n        } \n        /// you should add all other exceptions here that you want to handle\n\n        /// Don't forget to add general catch for all other exceptions\n        catch (e) {\n          return Response.serverError(body: \"Internal server error\"); \n          // for safery reasons we shouldn't return unknown exception to user\n        }\n    };\n}\n","language":"dart"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"class"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"ExceptionsAdapter"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"final"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Handler"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" _handler; "}]},{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"// this is the next handler in the chain"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"ExceptionsAdapter"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"this"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"._handler); "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[]},{"type":"element","tag":"span","props":{"class":"line"},"children":[]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"/// It will be called for every request, "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"/// and it will be responsible for handling exceptions above"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Handler"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"get"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"handler"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" => ("}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Request"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" request) "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"async"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"try"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"          "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"return"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"await"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"_handler"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"(request);"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        } "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"on"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"BadRequestException"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"catch"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" (e) {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"          "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"return"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Response"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"badRequest"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"(body: e.message);,"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"          );"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        } "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"on"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"NotAuthorisedException"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"catch"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" (e) {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"          "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"return"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Response"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":".("}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"            "}]},{"type":"element","tag":"span","props":{"style":{"color":"#B5CEA8"}},"children":[{"type":"text","value":"401"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"// Unauthorized http status code"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"            body: e.message);"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        } "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"/// you should add all other exceptions here that you want to handle"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"        /// Don't forget to add general catch for all other exceptions"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"catch"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" (e) {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"          "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"return"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Response"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"serverError"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"(body: "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"\"Internal server error\""}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"); "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"          "}]},{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"// for safery reasons we shouldn't return unknown exception to user"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        }"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    };"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"}"}]}]}]}]}]},{"type":"element","tag":"h2","props":{"id":"how-to-use-exceptions-adapter"},"children":[{"type":"text","value":"How to use Exceptions adapter"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Now we have created adapter but it now needs to be used in the pipeline."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"In a file with your server pipeline you need to add the following lines:"}]},{"type":"element","tag":"code","props":{"code":"  // this is your main handler that will be called for every request\n  final handler = MainController().handler; \n  // this is handler that will handle all exceptions\n  // it takes the next handler in the chain as an argument\n  final exceptionAdapterHandler = ExceptionsAdapter(handler).handler; \n\n  final _handler = Pipeline()\n      .addMiddleware(logRequests())\n      .addHandler(exceptionAdapterHandler);\n\n  final server = await serve(_handler, ip, port);\n","language":"dart"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"// this is your main handler that will be called for every request"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"final"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" handler = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"MainController"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"().handler; "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"// this is handler that will handle all exceptions"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"// it takes the next handler in the chain as an argument"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"final"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" exceptionAdapterHandler = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"ExceptionsAdapter"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"(handler).handler; "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"final"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" _handler = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Pipeline"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"()"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"      ."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"addMiddleware"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"logRequests"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"())"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"      ."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"addHandler"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"(exceptionAdapterHandler);"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"  "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"final"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" server = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"await"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"serve"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"(_handler, ip, port);"}]}]}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Our request will follow the following pipeline:"}]},{"type":"element","tag":"ol","props":{},"children":[{"type":"element","tag":"li","props":{},"children":[{"type":"text","value":"log middleware"}]},{"type":"element","tag":"li","props":{},"children":[{"type":"text","value":"exception adapter middleware"}]},{"type":"element","tag":"li","props":{},"children":[{"type":"text","value":"main controller handler"}]}]}],"toc":{"title":"","searchDepth":2,"depth":2,"links":[{"id":"introduction","depth":2,"text":"Introduction"},{"id":"what-is-an-adapter","depth":2,"text":"What is an Adapter?"},{"id":"preparations","depth":2,"text":"Preparations"},{"id":"how-to-init-the-exceptions-adapter","depth":2,"text":"How to init the Exceptions adapter"},{"id":"how-to-use-exceptions-adapter","depth":2,"text":"How to use Exceptions adapter"}]}},"_type":"markdown","_id":"content:blog:dart-shelf-exceptions-adapter.md","_source":"content","_file":"blog/dart-shelf-exceptions-adapter.md","_extension":"md"},{"_path":"/blog/how-to-make-an-event-bus-plugin-in-nuxt","_draft":false,"_partial":false,"_empty":false,"title":"How to make an event bus plugin in Nuxt","description":"Easy solution for creating an event bus in Nuxt.js","excerpt":{"type":"root","children":[{"type":"element","tag":"h2","props":{"id":"step-1-create-the-file-for-the-plugin"},"children":[{"type":"text","value":"Step 1: Create the file for the plugin"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"In the plugins directory of your Nuxt project create a file called bus.js, you can call this file whatever you want but it seems most logical just to call it bus.js."}]},{"type":"element","tag":"h2","props":{"id":"step-2-add-your-plugin-to-nuxtconfigjs"},"children":[{"type":"text","value":"Step 2: Add your plugin to nuxt.config.js"}]},{"type":"element","tag":"code","props":{"code":"plugins: [\n    '@/plugins/bus',\n],\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"plugins: [\n    '@/plugins/bus',\n],\n"}]}]}]},{"type":"element","tag":"h2","props":{"id":"step-3-time-to-code"},"children":[{"type":"text","value":"Step 3: Time to code"}]},{"type":"element","tag":"code","props":{"code":"import Vue from 'vue'\n        \nconst eventBus = {}\n        \neventBus.install = function (Vue) {\n    Vue.prototype.$bus = new Vue()\n}\n        \nVue.use(eventBus)\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"import Vue from 'vue'\n        \nconst eventBus = {}\n        \neventBus.install = function (Vue) {\n    Vue.prototype.$bus = new Vue()\n}\n        \nVue.use(eventBus)\n"}]}]}]},{"type":"element","tag":"h2","props":{"id":"step-4-now-use-it"},"children":[{"type":"text","value":"Step 4: Now use it!"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"The first thing you need to do is set up an event bus listener wherever you need it by doing the following:"}]},{"type":"element","tag":"code","props":{"code":"this.$bus.$on('test-event', ( payload ) => {})\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"this.$bus.$on('test-event', ( payload ) => {})\n"}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Now in another component you can emit an event like so:"}]},{"type":"element","tag":"code","props":{"code":"this.$bus.$emit( 'test-event', payload )\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"this.$bus.$emit( 'test-event', payload )\n"}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"It's that easy! You can now use this in your pages, components, layout, etc."}]}]},"mid":2,"lang":"en","tags":["Vue","Nuxt"],"ttr":15,"image":"https://images.unsplash.com/photo-1500462918059-b1a0cb512f1d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=987&q=80","date":"2021-06-17T21:00:00.000Z","body":{"type":"root","children":[{"type":"element","tag":"h2","props":{"id":"step-1-create-the-file-for-the-plugin"},"children":[{"type":"text","value":"Step 1: Create the file for the plugin"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"In the plugins directory of your Nuxt project create a file called bus.js, you can call this file whatever you want but it seems most logical just to call it bus.js."}]},{"type":"element","tag":"h2","props":{"id":"step-2-add-your-plugin-to-nuxtconfigjs"},"children":[{"type":"text","value":"Step 2: Add your plugin to nuxt.config.js"}]},{"type":"element","tag":"code","props":{"code":"plugins: [\n    '@/plugins/bus',\n],\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#C8C8C8"}},"children":[{"type":"text","value":"plugins"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":": ["}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"'@/plugins/bus'"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":","}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"],"}]}]}]}]}]},{"type":"element","tag":"h2","props":{"id":"step-3-time-to-code"},"children":[{"type":"text","value":"Step 3: Time to code"}]},{"type":"element","tag":"code","props":{"code":"import Vue from 'vue'\n        \nconst eventBus = {}\n        \neventBus.install = function (Vue) {\n    Vue.prototype.$bus = new Vue()\n}\n        \nVue.use(eventBus)\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"import"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"Vue"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"from"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"'vue'"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"const"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4FC1FF"}},"children":[{"type":"text","value":"eventBus"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" = {}"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"eventBus"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"install"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"function"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" ("}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"Vue"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":") {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4EC9B0"}},"children":[{"type":"text","value":"Vue"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"prototype"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"$bus"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"new"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"Vue"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"()"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"Vue"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"use"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"eventBus"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tag":"h2","props":{"id":"step-4-now-use-it"},"children":[{"type":"text","value":"Step 4: Now use it!"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"The first thing you need to do is set up an event bus listener wherever you need it by doing the following:"}]},{"type":"element","tag":"code","props":{"code":"this.$bus.$on('test-event', ( payload ) => {})\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"this"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"$bus"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"$on"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"'test-event'"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":", ( "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"payload"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" ) "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"=>"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" {})"}]}]}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Now in another component you can emit an event like so:"}]},{"type":"element","tag":"code","props":{"code":"this.$bus.$emit( 'test-event', payload )\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"this"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"$bus"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"$emit"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"( "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"'test-event'"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":", "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"payload"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" )"}]}]}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"It's that easy! You can now use this in your pages, components, layout, etc."}]}],"toc":{"title":"","searchDepth":2,"depth":2,"links":[{"id":"step-1-create-the-file-for-the-plugin","depth":2,"text":"Step 1: Create the file for the plugin"},{"id":"step-2-add-your-plugin-to-nuxtconfigjs","depth":2,"text":"Step 2: Add your plugin to nuxt.config.js"},{"id":"step-3-time-to-code","depth":2,"text":"Step 3: Time to code"},{"id":"step-4-now-use-it","depth":2,"text":"Step 4: Now use it!"}]}},"_type":"markdown","_id":"content:blog:how-to-make-an-event-bus-plugin-in-nuxt.md","_source":"content","_file":"blog/how-to-make-an-event-bus-plugin-in-nuxt.md","_extension":"md"},{"_path":"/blog/how-to-use-amazon-sdk-with-digital-ocean-spaces","_draft":false,"_partial":false,"_empty":false,"title":"How to use Amazon SDK with Digital Ocean spaces","description":"A simple tutorial on how to use the Amazon SDK with Digital Ocean spaces.","excerpt":{"type":"root","children":[{"type":"element","tag":"h2","props":{"id":"lets-start-out-with-the-code-well-be-using"},"children":[{"type":"text","value":"Let's start out with the code we'll be using"}]},{"type":"element","tag":"code","props":{"code":"    const spacesEndpoint = new AWS.Endpoint('sfo2.digitaloceanspaces.com');\n    const s3 = new AWS.S3({\n        endpoint: spacesEndpoint,\n        accessKeyId: 'YOUR ACCESS KEY HERE',\n        secretAccessKey: 'YOUR SECRET HERE'\n     });\n    var params = {\n        Bucket: 'YOUR DO SPACE NAME HERE',\n        Key: path + filename,\n        Body: rawBuffer,\n        ACL: \"public-read\"\n     };\n    s3.putObject(params, function(err, data) {\n        if (err) console.log(err, err.stack);\n        else{\n            let fileUrl = \"https://yourbucketname.sfo2.digitaloceanspaces.com/\"+path+filename\n            //This is the url to your uploaded file\n        };\n    });\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"    const spacesEndpoint = new AWS.Endpoint('sfo2.digitaloceanspaces.com');\n    const s3 = new AWS.S3({\n        endpoint: spacesEndpoint,\n        accessKeyId: 'YOUR ACCESS KEY HERE',\n        secretAccessKey: 'YOUR SECRET HERE'\n     });\n    var params = {\n        Bucket: 'YOUR DO SPACE NAME HERE',\n        Key: path + filename,\n        Body: rawBuffer,\n        ACL: \"public-read\"\n     };\n    s3.putObject(params, function(err, data) {\n        if (err) console.log(err, err.stack);\n        else{\n            let fileUrl = \"https://yourbucketname.sfo2.digitaloceanspaces.com/\"+path+filename\n            //This is the url to your uploaded file\n        };\n    });\n"}]}]}]},{"type":"element","tag":"h2","props":{"id":"the-explaination"},"children":[{"type":"text","value":"The explaination"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"const spacesEndpoint"}]},{"type":"text","value":" will be the region you choose for your Digital Ocean space, my region is San Fransico so you will see I have sfo2.digitaloceanspaces.com."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"accessKeyId"}]},{"type":"text","value":" and "},{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"secretAccessKey"}]},{"type":"text","value":" are both obtained by logging into your Digital Ocean account and clicking spaces then near the top right you will see manage keys. After click manage keys, Generate a new Spaces Access key."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"img","props":{"alt":"","src":"https://images.unsplash.com/photo-1633174524827-db00a6b7bc74?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2392&q=80"},"children":[]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"I've included a screenshot of what those keys look like here. The top is "},{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"accessKeyId"}]},{"type":"text","value":" and the bottom is "},{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"secretAccessKey"}]},{"type":"text","value":"."}]},{"type":"element","tag":"h2","props":{"id":"the-params"},"children":[{"type":"text","value":"The Params"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"Bucket"}]},{"type":"text","value":" will be the name of your Digital Ocean Space."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"Key"}]},{"type":"text","value":" will be the path where your file is saved at, and the file name you can see how this might look in the code block above."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"Body"}]},{"type":"text","value":" will be the raw data that makes up your image."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"ACL"}]},{"type":"text","value":" is where you can set whether or not the image can be viewed by anyone or if its private by doing 'public-read' or 'private'."}]}]},"mid":3,"lang":"en","tags":["JS"],"ttr":5,"image":"https://images.unsplash.com/photo-1633174524827-db00a6b7bc74?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2392&q=80","date":"2021-06-08T21:00:00.000Z","body":{"type":"root","children":[{"type":"element","tag":"h2","props":{"id":"lets-start-out-with-the-code-well-be-using"},"children":[{"type":"text","value":"Let's start out with the code we'll be using"}]},{"type":"element","tag":"code","props":{"code":"    const spacesEndpoint = new AWS.Endpoint('sfo2.digitaloceanspaces.com');\n    const s3 = new AWS.S3({\n        endpoint: spacesEndpoint,\n        accessKeyId: 'YOUR ACCESS KEY HERE',\n        secretAccessKey: 'YOUR SECRET HERE'\n     });\n    var params = {\n        Bucket: 'YOUR DO SPACE NAME HERE',\n        Key: path + filename,\n        Body: rawBuffer,\n        ACL: \"public-read\"\n     };\n    s3.putObject(params, function(err, data) {\n        if (err) console.log(err, err.stack);\n        else{\n            let fileUrl = \"https://yourbucketname.sfo2.digitaloceanspaces.com/\"+path+filename\n            //This is the url to your uploaded file\n        };\n    });\n","language":"javascript"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"const"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4FC1FF"}},"children":[{"type":"text","value":"spacesEndpoint"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"new"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4FC1FF"}},"children":[{"type":"text","value":"AWS"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"Endpoint"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"'sfo2.digitaloceanspaces.com'"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":");"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"const"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4FC1FF"}},"children":[{"type":"text","value":"s3"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"new"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#4FC1FF"}},"children":[{"type":"text","value":"AWS"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"S3"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"({"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"endpoint:"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"spacesEndpoint"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":","}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"accessKeyId:"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"'YOUR ACCESS KEY HERE'"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":","}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"secretAccessKey:"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"'YOUR SECRET HERE'"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"     });"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"var"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"params"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" = {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"Bucket:"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"'YOUR DO SPACE NAME HERE'"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":","}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"Key:"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"path"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" + "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"filename"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":","}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"Body:"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"rawBuffer"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":","}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"ACL:"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"\"public-read\""}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"     };"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"s3"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"putObject"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"params"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":", "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"function"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"err"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":", "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"data"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":") {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"if"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" ("}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"err"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":") "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"console"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#DCDCAA"}},"children":[{"type":"text","value":"log"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"("}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"err"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":", "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"err"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"."}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"stack"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":");"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        "}]},{"type":"element","tag":"span","props":{"style":{"color":"#C586C0"}},"children":[{"type":"text","value":"else"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"{"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"            "}]},{"type":"element","tag":"span","props":{"style":{"color":"#569CD6"}},"children":[{"type":"text","value":"let"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"fileUrl"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":" = "}]},{"type":"element","tag":"span","props":{"style":{"color":"#CE9178"}},"children":[{"type":"text","value":"\"https://yourbucketname.sfo2.digitaloceanspaces.com/\""}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"+"}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"path"}]},{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"+"}]},{"type":"element","tag":"span","props":{"style":{"color":"#9CDCFE"}},"children":[{"type":"text","value":"filename"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"            "}]},{"type":"element","tag":"span","props":{"style":{"color":"#6A9955"}},"children":[{"type":"text","value":"//This is the url to your uploaded file"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"        };"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"style":{"color":"#D4D4D4"}},"children":[{"type":"text","value":"    });"}]}]}]}]}]},{"type":"element","tag":"h2","props":{"id":"the-explaination"},"children":[{"type":"text","value":"The explaination"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"const spacesEndpoint"}]},{"type":"text","value":" will be the region you choose for your Digital Ocean space, my region is San Fransico so you will see I have sfo2.digitaloceanspaces.com."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"accessKeyId"}]},{"type":"text","value":" and "},{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"secretAccessKey"}]},{"type":"text","value":" are both obtained by logging into your Digital Ocean account and clicking spaces then near the top right you will see manage keys. After click manage keys, Generate a new Spaces Access key."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"img","props":{"alt":"","src":"https://images.unsplash.com/photo-1633174524827-db00a6b7bc74?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2392&q=80"},"children":[]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"I've included a screenshot of what those keys look like here. The top is "},{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"accessKeyId"}]},{"type":"text","value":" and the bottom is "},{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"secretAccessKey"}]},{"type":"text","value":"."}]},{"type":"element","tag":"h2","props":{"id":"the-params"},"children":[{"type":"text","value":"The Params"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"Bucket"}]},{"type":"text","value":" will be the name of your Digital Ocean Space."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"Key"}]},{"type":"text","value":" will be the path where your file is saved at, and the file name you can see how this might look in the code block above."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"Body"}]},{"type":"text","value":" will be the raw data that makes up your image."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"strong","props":{},"children":[{"type":"text","value":"ACL"}]},{"type":"text","value":" is where you can set whether or not the image can be viewed by anyone or if its private by doing 'public-read' or 'private'."}]}],"toc":{"title":"","searchDepth":2,"depth":2,"links":[{"id":"lets-start-out-with-the-code-well-be-using","depth":2,"text":"Let's start out with the code we'll be using"},{"id":"the-explaination","depth":2,"text":"The explaination"},{"id":"the-params","depth":2,"text":"The Params"}]}},"_type":"markdown","_id":"content:blog:how-to-use-amazon-sdk-with-digital-ocean-spaces.md","_source":"content","_file":"blog/how-to-use-amazon-sdk-with-digital-ocean-spaces.md","_extension":"md"}]