NestJS: where to place a file reader? - node.js

I have an architectural question regarding NestJS. Let's say I have a ContentModule and a BlueprintModule, both need to read json files. In both cases I would have a service that calls the repository which than should read those files. This repository should now use something like a FileReader class.
Where would i place this in NestJS? Should I create a new module for this and inject it into the ContentModule and BlueprintModule? I am a bit confused because I see this more as a util than a feature module. Should I just create a simple class in a utils or lib folder?
Would I create a repository at all if I don't have a database and instead access a JsonService from my BlueprintService and ContentService?
Any thoughts?

You can create a simple class in Utils and inject to your Contentmodule and Blueprint module.
There is no hard and strict rule to this. You can start off by adding it in a libs and then as you find more use cases you can always move it to another module.
Basic rule is, if you are not sure where things are to be placed, they get placed in libs. :)
Thanks

Related

Are Entities between two sub-apps in NestJs sharable?

I am very new to NestJS and I am trying to create a project with multiple subapps. The query that I have is, if one entity is present in one sub-app, can we use that same entity in another sub-app of the same project?
This is my project strucutre:
apps
subApp1
entities
- entityone.ts
controller, modules and services
subApp2
entities
- entityTWo.ts
controller, modules and services
I want to use the entity one in subApp2 service. Is it possible? If so, how we can acheive this? Is there any better way to approach this situation where I need to use Entity of one sub app into another.
I tried exporting the Entity from one sub-app and import the Module in another sub app. But I am getting this error:
Nest cannot export a provider/module that is not a part of the currently processed module (StoreModule). Please verify whether the exported Store is available in this particular context.
I am not sure, if it was the correct way for this situation, but this solution came into my mind and i tried it.
you don't need to specify entities for every module.
keep them all together apart, so you can use them wherever you want.

Which function in "nestjs/swagger" converts DTOs to Swagger model definitions?

I have DTOs specified with Class-Validator and I am looking for a library that can be used to generate Swagger specification from it. I am not using it for a REST API, the code is addressing an IoT/MQTT scenario - I simply use Class-Validator to manage JSON.
NestJS/Swagger is the best maintained library. I would like to use it's capability to produce Swagger definitions without a NestJS Server. Ideally I would like to pass in a DTO definition and get it's Swagger schema.
I have been reading the source, but am struggling to understand which function in the framework actually does that. At best, I have been able to track it down to modelsDefinitions property in swagger-explorer class.
As best I can tell, from there, api-parameters.explorer and api-produces.explorer. The way they work is not clear to me. I was wondering of someone might help me out?
I'd like to add that I am aware of class-validator-jsonschema, but it is not maintained and no longer seems to work properly.
nestjs/swagger does not expose what you need as its public API which you cannot access it. The class you're looking for is SchemaObjectFactory and the method is exploreModelSchema.
Reference:
SwaggerObjectFactory
Test

Openapi/Swagger generation from Typescript code

I am looking for a way to generate OpenAPI/Swagger API definitions from code written in Node.JS/Express.JS/Typescript.
Ideally this would be just annotations the I had to my Express Typescript base controllers and this generate the OpenApI/Swagger by running some sort of command line, this way the API definition would always stay in sync with the actual implementation, and additionally this tool should generate some sort of middleware that can implement parameter validation of the controllers input parameters.
Thank you, kindly
Oscar
If you want swagger generated for you, you can make use of NestJs.
NestJs also provides inbuilt middle ware for validation.
If you wanna know more visit this link

Silex / Symfony2 Remember Me Authentication User Interface RedBean Wrapper

I've been trying to use RedBean ORM (http://redbeanphp.com) to implement UserInterface and UserProviderInterface of the Silex Security Provider Package.
Because of the way the RedBean ORM handles functions for its objects, I've needed to wrap the bean object in another class.
This works great for authentication, but fails tests for Remember Me functionality.
I noticed that somewhere along the chain the Security Package serializes the object.
I thought maybe this was the reason for the error, so I created properties for "id" and "password" in my wrapper class and used __sleep and __wakeup methods to ignore the bean during sleep and reload it on wakeup. Despite everything seeming to load properly during __wakeup the test for "Remember Me" functionality is still failing.
I have created a github repository of my code. If anyone has any ideas, I'd much appreciate it!
For some reason RedBean, Silex and PHPUnit aren't allowing themselves to be included in the repository. A simple composer update should pull them down for you. If anyone has any ideas why, I'd appreciate an answer to that as well.
The github repository can be found at:
https://github.com/christianmagill/silex-redbean-security
The applicable files are
To create the test user in the database:
/setup.php
To run the test:
/index.php
My implementation of UserInterface:
/src/App/Model/UserSecurityWrapper.php
My implementation of UserProviderInterface:
/src/App/Model/UserProvider.php
My modified test:
/src/App/Test/RememberMeRedBeanServiceProviderTest.php
The original test:
/vendor/silex/silex/tests/Silex/Tests/Provider/RememberMeServiceProviderTest.php
The problem was with my custom UserProvider's supportsClass method. I was not taking namespacing into account. It seems like this function is not called for basic authentication, but is needed for the remember me provider.

Orchard CMS Custom Data Importer

I have custom code that reads a bunch of xml documents and creates a custom data import recipe that I upload using the Import/Export module in Orchard. The imported documents are of a content type "Api Documentation" that I created using the Orchard admin UI. Now, I would like to make this recipe file generation code part of a custom Orchard module (if that is the right approach, I am not sure) and let the admin user do the following:
Use a form in the admin Dashboard section to upload the xml documents that need to be run through the recipe file generator
That form submits the recipe file to the Import/Export module, so that it can perform its import process as usual
What would be the best approach to handle this? I am not even sure that creating a custom module is the right approach. If there are other extensibility options that I should take advantage of, that would be great to know.
The IImportExportService interface has a member called Import:
void Import(string recipeText);
Once you've generated your recipe you can call this method and it will execute the recipe and update the shell.
If you are just importing data then if I'm not mistaken you can get away without updating the shell (which you only need to do if features have been enabled or disabled) - in which case you can make a couple of calls to the IRecipeParser and IRecipeManager interfaces instead:
var recipe = _recipeParser.ParseRecipe(recipeText);
_recipeManager.Execute(recipe);
I've written something similar which does some importing/exporting so you can move pieces of content between sites; it also does some encryption so the details are hidden from prying eyes. A great place to start if is to read the source for the ImportExportService as it isn't all that complicated.

Resources