How to use local JSON assets to simulate API in Scala.js - json

I'm new to Scala and Scala.js and I want to experiment with handling JSON data. I'd like to simulate a server response by returning the content of a JSON file local to my Scala.js project, parse it and work with the data. What would be the best way to do so? Where should I place these files in my project tree, and how would I get their content?
Say that I have a file called myJSON.json containing something like
[
{
"ress": "AR",
"lastDate": "2017-10-27 09:19:18"
},
{
"ress": "JIM",
"lastDate": "2017-10-27 06:57:15"
},
{
"ress": "JOE",
"lastDate": "2017-09-29 11:57:39"
}
]
Can I place this file somewhere in my project so that I can read this file and then parse its content to use it somehow (could be displayed in the browser, logged to the console, etc...)? I guess I could use a tool such as scala-js or something similar for parsing, but accessing the file content in the first place is what I try to figure out.
Note that I'm using scala-js.
Thanks in advance!

Like others said above, Javascript that runs in the browser in general can't access the local filesystem. There are some exceptions:
The File API lets you access files that the user has selected in the UI using <input type="file" /> or drag-and-dropped into the browser window.
The Filesystem API lets you access files the way you seem to want, but it is non-standard and is not supported in most browsers. It also seems that Scala.js has no typings for it, but I'm not sure.
scala-js-dom has typings for the File API that you can use – search for File and FileList types in its source. Its API mirrors the Javascript API, so you will need to look for how exactly to do this in JS. Then translating it into Scala.js will be easy (or at least a different question).
If the File API does not work for your use case, another option is to use something like json-server to easily serve your JSON files on localhost via HTTP.

Related

How do I populate MongoDB with my CDN Links?

I have a JSON file, and I have some audio file in each entry.
{
{
audio: '~/audios/1.mp3'
info: 'some other info'
},
{
audio: '~/audios/2.mp3'
info: 'some other info'
},
{
audio: '~/audios/3.mp3'
info: 'some other info'
}
}
Now I would like to put all of this stuff in my MongoDB database (instead of using this JSON). In the very end my app will be using some service to store the mp3 files on some super-efficient server I guess, so I would need to save their proper links in my MongoDB. So I guess I will have links like https://cdnjs.cloudflare.com/bla/data/audio1.mp3 (for example) - But how do I generate these links and pop them into my MongoDB database?
I'm not sure if I understand your question. Just upload your audio to your CDN. It should generate the links for you. You can save these to MongoDB by interfacing directly with the Mongo shell or using the Mongoose ORM. If users are going to be uploading music to your app directly, you will probably be using some external API to upload files. For example, if you wanted to upload Images to the Imgur API, you would send data to their API endpoints for image uploads and their API would automatically return a link to your image. You would need to write a callback function that checks whether the image upload went correctly - if all went well and you don't need to throw an error, you would have a method written in your callback to create a new document in MongoDB/Mongoose to save that link, following a schema that makes it logically possible to retrieve the location/uploader (also saving a reference to the user who uploaded it, for example)
You would also probably be using HTML's to handle this, if it's a web app
Alternatively, you can set up your own methods on your back-end server for storing and retrieving file uploads, hosting on Amazon will give you a lot of bandwidth to work with.

Open local JSON file for examination

I was wondering if it's possible to open a local JSON file so I can just check its structure? Didn't/don't want to upload the file to an online JSON format checker site and was hoping I can just utilize PAW to do that.
Don't seem to be able to do this with a local file, unless I run it through a local server, eg using MAMP, unless I missed something...?
Thanks.
You could copy the content into the txt body then switch to the JSON body this will let you view it in the nice structure, sorry currently no way to directly import a file need to copy past the content.
Take a look at jsonlint npm module. Supports JSON schema validation and pretty printing.

Add API documentation to website using Swagger document

I have a basic webpage built, and a Swagger document JSON file of my API. I am unsure of how to actually add the data from the document to the website so that it can be browsed.
I want to build hosted documentation for the API.
This is the example given by Swagger: http://petstore.swagger.wordnik.com/#!/pet/addPet
Do I just download Swagger UI and use it in conjunction with the JSON file.
But I am unsure on to achieve this. Any advice on how to go about creating something like this would be very helpful.
Swagger-ui is basically a set of static files you can host on your server to display your API.
Unless you may any major changes, you just need to copy the contents of the /dist folder to your server and host it as part of your application (or static website, doesn't matter).
The SwaggerUi object can be customized to your needs, including the URL of the spec you're hosting.
Keep in mind that if you don't host the ui and spec on the same server, (that is, same host and same port), you need to enable CORS.

Swagger API Specification filenames

I'm trying to use Swagger to create API documentation for an API we're building and I've never used it before.
The documentation on Github says that the Resources Listing needs t be at /api-docs and the various resource files need to be at /api-docs/books etc.
This makes naming files and folders very tricky. I think they expect the files to have no file names, rather than having a folder called /api-docs it has to be an extension-less file, then you can't put the resources in an api-docs folder because you can't call the folder that, so they suggest using a folder called /listings.
This folder doesn't appear in the URL structure of your documentation though, it's kind of invisible because you set the baseURL in your resources to the proper path, but it looks like that has to be an absolute path, which is awkward if you want to have it on several servers (local and production).
Maybe I just don't get it but this all seems to be absolutely nuts.
So, I have 2 questions.....
1) Can I give my resource listing file and my resource files a .json extension? This would make sense as it's a JSON file.
2) Can I use a relative path to the resource listing file in the baseURL in my resource files?
Ideally, my file structure would be flatter, like this...
/api-docs
resources.json
books.json
films.json
Is Swagger flexible enough to do this?
It's an IIS server if that makes any difference (if the solution requires routing for example).
I was able to put model files into a folder under the web root and could reference them like this.
$ref: '/models/model.yml#/MyObject'
Relative paths also worked without a leading slash.
$ref: 'models/model.yml#/MyObject'
Inside the model.yml, I can reference other objects int eh same file like this
$ref: '#/MyObject2'.
However, I could only get the main swagger file to import model files. I could not get one model file to cross-reference another model file.
I was using a Tomcat web server but the principle will be the same.

How to use the dart:html library to write html files?

I want to make a program that prepares an HTML file. It would either be on the server side or just running in my local machine.
I think it would be nice to be able to use the dart:html library since it has a lot of methods for manipulating html (obviously). But it is thought to be used dynamically on the client side, and I want to use it like this: manipulate an html DOM tree with dart:html, and when its ready, write a static html file. For instance using query('body').innerHtml
The problem I'm running into is that I if start a project with the "console application" template, I am not able to make dart:html talk to an html file. And if I choose "web application", in which I am able to do this, I cannot load the dart:io library, maybe it has to do with it being tagged as [server] in the SDK?
Of course I could just do:
print(query('body').innerHtml);
and manually copying the output to a file, but I thought maybe there is a more elegant solution.
See html5lib.
html5lib in Pure Dart
This is a pure Dart html5 parser. It's a port of
html5lib from Python. Since it's 100% Dart you can use it safely from
a script or server side app.
Eventually the parse tree API will be compatible with dart:html, so
the same code will work on the client or the server.
It doesn't support much in the way of queries yet.