• Semo has been in the works for two years, and today we are introducing the second article in our Semo series: the Semo-plugin-read widget
  • The Semo plugin-read widget is the first in the Semo series
  • Semo: The Semo-Plugin-Chalk plug-in

Today I’m going to write part 4, which will show you a new Semo plugin that I spent four hours writing yesterday, and the next part will show you the REPL environment for Semo

Functional description

Since Semo itself does not provide any direct functionality, it is just a specification and constraint for command-line development, so if you want some functionality, you need to extend it based on Semo. Today this plug-in provides Semo with network access capabilities based on the popular AXIos NPM package.

You might be thinking, why would I use this plugin when I just use Axios when I want to use it? Good question, of course I could use Axios directly, but there’s also the added benefit of having a command-line tool like Curl.

Let’s talk about installation and usage

The installation

Assuming you don’t already have Semo installed, do

npm i -g @semo/cli semo-plugin-api
semo api help
Copy the code

When you run help, you’ll see a whole bunch of arguments. Don’t be surprised.

usage

Here, let’s take the Fake Rest Server, which is publicly available online, as an example

A GET request

The default is GET, so no additional arguments are required.

 $ semo api https://jsonplaceholder.typicode.com/posts/1 

{
  userId: 1,
  id: 1,
  title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
  body: 'quia et suscipit\n' +
    'suscipit recusandae consequuntur expedita et cum\n' +
    'reprehenderit molestiae ut ut quas totam\n' +
    'nostrum rerum est autem sunt rem eveniet architecto'
}
Copy the code

A POST request

As you can see here, we use -x for the request verb, we can also use –method, and we use –data for the request body. Note that we can construct an object structure with a single command.

 $ semo api -X POST https://jsonplaceholder.typicode.com/posts --data.title=foo --data.body=bar --userId=1   

{ title: 'foo', body: 'bar', id: 101 }
Copy the code

PUT request

Similarly, PUT is a substitution, and the rest is similar to POST, but notice that we use a lower case PUT, which means that the verb is not case sensitive.

 $ semo api -X put https://jsonplaceholder.typicode.com/posts/1 --data.title=foo --data.body=bar --userId=1 --id=1  

{ title: 'foo', body: 'bar', id: 1 }
Copy the code

PATCH request

PATCH is used to replace some of them

 $ semo api -X patch https://jsonplaceholder.typicode.com/posts/1 --data.title=foo    

{
  userId: 1,
  id: 1,
  title: 'foo',
  body: 'quia et suscipit\n' +
    'suscipit recusandae consequuntur expedita et cum\n' +
    'reprehenderit molestiae ut ut quas totam\n' +
    'nostrum rerum est autem sunt rem eveniet architecto'
}
Copy the code

The DELETE request

When I DELETE, the Rest Server does not return anything to me, just no error

semo api -X delete https://jsonplaceholder.typicode.com/posts/1 
{ status: 200, statusText: 'OK' }
Copy the code

HEAD request and OPTIONS request

These two can also be emitted without any obvious response.

 $ semo api -X head https://jsonplaceholder.typicode.com/posts/1  

{ status: 200, statusText: 'OK' }

 $ semo api -X options https://jsonplaceholder.typicode.com/posts/1

{
  status: 204,
  statusText: 'No Content'.'access-control-allow-credentials': 'true'.'access-control-allow-methods': 'GET,HEAD,PUT,PATCH,POST,DELETE'
}
Copy the code

I’m not satisfied with the response to either request, and may need to refine it later.

Support sending--headers

There are a lot of parameters that are supported, but most of them are really not very commonly used, so you can read the Axios package description if you need to.

semo api https://jsonplaceholder.typicode.com/posts/1 -H 'X-A:B'
semo api https://jsonplaceholder.typicode.com/posts/1 -H.'X-A'=B
Copy the code

Obviously the second style is weird, so the first style is recommended.

Some other function points

Pass parameters through files

With so many parameters, it’s too tiring to write every time. I can make it a JS file or a JSON file

semo api --file api.js
semo api --file api.json
Copy the code

The original -x and URL can be written or not, and can be written in a file. Also, if it is js, the module can return literal objects or functions, or promises.

Add color to the JSON response

Of course, some terminals will be colorized even without –color. If –color is used here, Semo utils.log will be used.

semo api https://jsonplaceholder.typicode.com/posts/1 --color
Copy the code

Access to the Proxy

What if the interface you’re testing is not accessible, and there’s parameter support

semo api http://myip.ipip.net semo api http://myip.ipip.net --ss semo api http://myip.ipip.net - socks proxy = socks: / / 127.0.0.1:1080Copy the code

As you can see, the return is different with or without the –ss parameter. In addition, if your port happens to be on 1080, you don’t need to enter this long to execute the specific port.

Debugging information

You can use the debug mechanism to view hidden debugging information:

DEBUG=semo-* semo api https://jsonplaceholder.typicode.com/posts/1
Copy the code

Implicit perform

If you don’t want to install the plugin globally or within your project, you still want to use it

semo run api -- https://jsonplaceholder.typicode.com/posts/1
Copy the code

Semo Run automatically downloads the plugin to another location for you. It only works when you use it. Semo Help doesn’t normally see it.

Don’t want to installSemoBut wanted to give the plugin a try

This way you have to implicitly download Semo every time, which is actually quite slow, just for the demo.

npx @semo/cli run api -- http://myip.ipip.net
Copy the code

Provide services as packages

import { api } from 'semo-plugin-api'
API = api('debug-prefix')
await API.get(url, config)
await API.post(url, data, config)
Copy the code

or

const { api } = await Utils.invokeHook('component')
Copy the code

summary

This is yet another plug-in that was completed in a few hours, using someone else’s core, to show the capabilities and uniform style of Semo encapsulation, and the fact that such a small plug-in will serve as a building block for a larger project that will be integrated later.

My vision is to form a small community where we can build a lot of plug-ins together. Some of them are building blocks for larger projects, some are private modules within the organization, and some are just for fun.

semo run plugin -- --remote
Copy the code

You can see that the current plug-in is still very few, I still need to continue to work on ha, hope to get your support.