New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json2http

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json2http - npm Package Compare versions

Comparing version
0.0.3
to
1.1.0
+18
types/src/kotlin/code.d.ts
import { Json2classKotlin } from 'json2class';
import * as Base from '../base';
export declare class Complex extends Json2classKotlin.Complex {
}
export declare class Simple extends Json2classKotlin.Simple {
}
export declare class Http extends Base.Http<Complex, Simple> {
toLaunch(plan: Base.SchemaPlan): {
code: string;
plan: string;
};
static get agentConfig(): {
name: string;
import: string;
code: string;
};
static toEntry(): string;
}
export * from './code';
import { Json2classSwift } from 'json2class';
import * as Base from '../base';
export declare class Complex extends Json2classSwift.Complex {
}
export declare class Simple extends Json2classSwift.Simple {
}
export declare class Http extends Base.Http<Complex, Simple> {
toLaunch(plan: Base.SchemaPlan): {
code: string;
plan: string;
};
static get agentConfig(): {
name: string;
import: string;
code: string;
};
static toEntry(): string;
}
export * from './code';
+1
-1
{
"name": "json2http",
"version": "0.0.3",
"version": "1.1.0",
"description": "json2http is a CLI tool that depends on json2class for generating code from JSON or JSON5, enabling HTTP-based requests.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -5,15 +5,15 @@ # json2http

json2http is a CLI tool that relies on json2class to convert a specified JSON(5) file into HTTP request code.
json2http is a command-line tool that relies on [json2class](https://github.com/yangfanzn/json2any/blob/main/packages/json2class/README.md) to convert specified JSON(5) files into HTTP request code.
## Supported Languages
### Currently Supported
| [arkTs@12](https://developer.huawei.com/consumer/cn/arkts/) | [dart@3](https://dart.dev/) | [typescript@5](https://www.typescriptlang.org/) |
| [dart@3](https://dart.dev/) | [arkTs@12](https://developer.huawei.com/consumer/cn/arkts/) | [typescript@5](https://www.typescriptlang.org/) | [kotlin@1.3](https://kotlinlang.org/) | [swift@5.7](https://developer.apple.com/swift/) |
### Planned Support
| [java](https://dev.java/) | [kotlin](https://kotlinlang.org/) | [swift](https://developer.apple.com/cn/swift/) | [Other languages to be supported]() |
| [java](https://dev.java/) | [Other languages to be supported]() |
## Installation
### `✅ Recommended` Node, npm, and npx Development Environment
npx requires a Node environment. Please install Node first.
- ### `✅ Recommended` Node, npm, and npx Development Environment
> npx requires a Node environment. Please install Node first.
```sh

@@ -23,3 +23,3 @@ npx json2http build -l dart@3

### Flutter and Dart Development Environment
- ### Flutter and Dart Development Environment
```sh

@@ -30,11 +30,16 @@ dart pub add dev:json2http

### HarmonyOS Development Environment
Add the following configuration to oh-package.json5.
- ### HarmonyOS Development Environment
> _Due to the release restrictions of `OpenHarmony Third-party Library Center`, from version `v0.0.14`,
> independent executable files are no longer provided. Instead, JS scripts are provided and executed by `node`.
> Fortunately, `OpenHarmony Developer Tools` and `DevEco-Studio` come with `node`. Just add it to the `PATH` environment variable._
> - _`OpenHarmony Developer Tools`'s `node` is usually at: command-line-tools/tool/node/bin/node_
> - _`DevEco-Studio`'s `node` is usually at: DevEco-Studio.app/Contents/tools/node/bin/node_
>
> _The above methods are a bit cumbersome, so it is still recommended to use the npx method for simplicity._
> Add the following configuration to oh-package.json5.
```json5
{
"scripts": {
// Windows system
"json2http": "./oh_modules/json2class/src/main/resources/rawfile/json2http-win.exe build -l arkTs@12",
// macOS system
"json2http": "./oh_modules/json2class/src/main/resources/rawfile/json2http-macos build -l arkTs@12"
"json2http": "node ./oh_modules/json2http/src/main/resources/rawfile/json2http build -l arkTs@12",
}

@@ -50,13 +55,499 @@ }

## Quick Start
JSON files support both json and json5 formats.
```json5
// ~/projects/config/root.json
{
'/api/blood/index': {
title: 'Baby blood type calculation',
method: 'GET',
params: {
father: '',
mother: '',
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
```
筹备发布中
By default, it searches for and converts json configurations in the current directory where the command is executed.
```sh
cd ~/projects/config/
npx json2http build -l dart@3
```
In preparation for release
Usage of Code
```dart
import 'json2http.dart';
<!-- ohpm install json2http -->
main() async {
// Global configuration
Json2http.setPlan = (p) {
p.baseURL = 'https://qqlykm.cn';
p.process = (reply) {
final data = reply.data;
if (data is Map) {
if (data['code'] != 200) {
// Set error to determine whether to throw an exception
// Avoid checking status code for every interface
reply.error = data['msg'];
}
}
};
};
## JSON Configuration Guide
// Initiate request
final plan = await Json2http.single.apibloodindex((p) {
// Parameter configuration for a single request
p.params.father = 'A';
p.params.mother = 'O';
});
// Return value
print(plan.res.toJson());
print(plan.res.msg);
print(plan.res.data.possible.elementAtOrNull(0));
print(plan.res.data.impossible.elementAtOrNull(0));
}
```
## JSON Configuration
### A Complete HTTP Request Configuration
```json5
{
'/api/test/path': {
title: 'Short description of the interface',
method: 'POST',
headers: { 'x-some-key': '' },
params: {
args1: '', args2: '', args3: '...'
},
body: {
type: 'json',
data: {
args4: 0, args5: '', args6: true, args7: [''],
},
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
```
### /api/test/path
- Description: Configure the interface path address
- Validation: Must start with "/"
- Required: Yes
### title
- Description: Configure a short description of the interface
- Validation: Must be a string
- Required: Yes
### method
- Description: http method
- Validation: GET, POST, DELETE, PUT, etc.
- Required: Yes
### headers
- Description: http headers
- Validation: Must be map<string, string or Array<string>> type
- Required: No
### params
- Description: http path query parameters
- Validation: Must be map<string, string | number | boolean> type
- Required: No
- Example:
```json5
{
'/api/test/path': {
title: '',
method: 'GET',
params: { xxx: '', yyy: 123, zzz: true },
},
}
```
### body
- Description: http body configuration
- Validation: Can only configure type and data fields under it
- Required: No
### body.type
- Description: http body type
- Validation: json, map, form, plain, byte
- Required: Yes
### body.data [body.type = json]
- Validation: data can be any valid json data
- Required: Yes
- Example:
```json5
{
'/api/test/path1': {
title: '',
method: 'POST',
body: {
type: 'json',
data: 1,
},
},
'/api/test/path2': {
title: '',
method: 'POST',
body: {
type: 'json',
data: {
xxx: 0, yyy: [''], zzz: true
},
},
},
}
```
### body.data [body.type = map]
- Validation: data must be map<string, string | number | boolean> type
- Required: Yes
- Example:
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'map',
data: { xxx: '', yyy: 123, zzz: true },
},
},
}
```
### body.data [body.type = form]
- Validation:
1. data must contain fields and files fields
2. files field must be map<string, string | Array<string>> type
3. fields field must be map<string, (string | number | boolean) | Array<string | number | boolean>> type
- Required: Yes
- Example:
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'form',
data: {
fields: { xxx: '', yyy: 123 },
files: { aaa: [true], bbb: '' },
},
},
},
}
```
### body.data [body.type = plain]
- Validation: data must be a string
- Required: Yes
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'plain',
data: '',
},
},
}
```
### body.data [body.type = byte]
- Validation: data must be a string
- Required: Yes
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'byte',
data: '',
},
},
}
```
### res
- Description: http return value
- Validation: Any valid json object
- Required: No
- Example:
```json5
{
'/api/test/path': {
title: '',
method: 'GET',
res: {
statusCode: '',
statusMessage: '',
data: { xxx: '', yyy: 1 },
},
},
}
```
## Other Configurations
### Path Parameters
```json5
{
'/api/test/{user}': {
title: '',
method: 'GET',
},
}
```
```dart
main() async {
await Json2http.single.apitestuser((p) {
p.seg.user = 'user';
});
}
```
### Custom Method Name
If you really don't like using the interface address as the method name, you can also customize the method name in this way.
```json5
{
apiMethodName: {
path: '/api/test/path',
title: '',
method: 'GET',
params: { xxx: '' },
},
}
```
```dart
main() async {
await Json2http.single.apiMethodName((p) {
p.params.xxx = 'user';
});
}
```
### Rely on json2class to generate objects
- params
- body.data
- body.data.fields
- body.data.files
- res
The code generated for the above fields will inherit `json2class`, and the configuration and usage of the corresponding values follow [json2class](https://github.com/yangfanzn/json2any/blob/main/packages/json2class/README.md), such as:
- How to set fields as optional
- How to set filling methods
- The usage of the generated code is also completely consistent
### Referencing an Existing Structure
Similar to `json2class`, the configuration of `json2http` can also reference an existing structure through `{ $meta: { ref: '' } }`.
The difference is that when referencing a parent structure different from the current one, `json2class` uses the file as the basic unit, while `json2http` uses the `interface` as the basic unit.
In the following example, `res` in interface `/api/test/path2` can reuse `res` in `/api/test/path1`.
```json5
{
'/api/test/path1': {
title: '',
method: 'GET',
res: {
xxx: '',
},
},
'/api/test/path2': {
title: '',
method: 'GET',
res: {
$meta: { ref: '/api/test/path1#/res' },
},
},
}
```
If the reference occurs in the current interface, the current interface address can be omitted.
```json5
{
'/api/test/path': {
title: '',
method: 'GET',
params: { xxx: '' },
res: {
$meta: { ref: '#/params' },
},
},
}
```
By referencing its own parent, recursive types can be generated.
```json5
{
'/api/test/path': {
title: '',
method: 'GET',
res: {
someKey: '',
child: {
: { ref: '#/res' },
},
},
},
}
```
### Setting Parameters Optional
- body.data [body.type = json]
- body.data [body.type = byte]
- body.data [body.type = plain]
In these three cases, you can set `?` mark for data, and the generated code can set data to `null`.
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'json',
'data?': 1,
},
},
}
```
```dart
main() async {
await Json2http.single.apitestpath((p) {
p.body.data = null;
});
}
```
## Usage of Generated Code
### Global Entry Json2http
- Json2http.setPlan
```dart
import 'json2http.dart';
// You can customize the Agent to fully control the request call behavior
class XAgent extends Agent {
Future<Reply> fetch(Plan plan) async {
// Can be implemented based on the generated code
plan.reply.data = {'statusCode': '0'};
plan.reply.code = 200;
return plan.reply;
}
body(Plan plan) {
// Can be implemented based on the generated code
return null;
}
}
main() {
Json2http.setPlan = (plan) {
// Global configuration
plan.baseURL = 'http://localhost:3000';
plan.process = (reply) {
final data = reply.data;
if (data is Map) {
if (data['statusCode'] != '0') {
// Determine whether the final request throws an exception
reply.error = data['statusMessage'];
}
}
};
plan.ready = () {
// Set the original configuration in the original request tool
plan.option = xxx;
// Set the original request tool
plan.session = xxx;
};
if (plan is testagentplan) {
plan.agent = XAgent();
}
if (plan.title.startsWith('amap:')) {
// Special interface special configuration
plan.baseURL = 'https://restapi.amap.com/v3';
plan.path = '${plan.path}?key=xxx';
plan.process = (reply) {
var data = reply.data;
if (data is Map) {
if (data['status'] != '1') {
reply.error = data['info'];
}
}
};
}
if (plan is refreshtestplan) {
plan.after = () async {
if (plan.reply.error == 'small token is error') {
// Implement token refresh
final x = await Json2http.single.refreshtoken((p) {
p.headers = {'access-token': 'bearer xxx'};
});
// After re-setting the token, retry the interface
plan.headers['access-token'] = x.res.data;
await plan.fetch();
}
};
}
};
}
```
- Initiate interface call
```json5
{
'/api/blood/index': {
title: 'Baby blood type calculation',
method: 'GET',
params: {
father: '',
mother: '',
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
```
```dart
main() async {
final result = await Json2http.single.apibloodindex((plan) {
plan.params.father = 'A';
plan.params.mother = 'B';
});
}
```
## Additional Command-Line Options

@@ -87,4 +578,3 @@ ### -l, --language: Specifies the target language for the build.

## Feedback & Improvement
Thank you for using this tool! In order to quickly improve and release **version 1.0.0**,
we would love to hear your feedback and suggestions.
Thank you for using this tool! we would love to hear your feedback and suggestions.
If you encounter any issues or have suggestions for improvement,

@@ -91,0 +581,0 @@ please feel free to provide feedback through the following channels:

+508
-18

@@ -5,15 +5,15 @@ # json2http

json2http is a CLI tool that relies on json2class to convert a specified JSON(5) file into HTTP request code.
json2http is a command-line tool that relies on [json2class](https://github.com/yangfanzn/json2any/blob/main/packages/json2class/README.md) to convert specified JSON(5) files into HTTP request code.
## Supported Languages
### Currently Supported
| [arkTs@12](https://developer.huawei.com/consumer/cn/arkts/) | [dart@3](https://dart.dev/) | [typescript@5](https://www.typescriptlang.org/) |
| [dart@3](https://dart.dev/) | [arkTs@12](https://developer.huawei.com/consumer/cn/arkts/) | [typescript@5](https://www.typescriptlang.org/) | [kotlin@1.3](https://kotlinlang.org/) | [swift@5.7](https://developer.apple.com/swift/) |
### Planned Support
| [java](https://dev.java/) | [kotlin](https://kotlinlang.org/) | [swift](https://developer.apple.com/cn/swift/) | [Other languages to be supported]() |
| [java](https://dev.java/) | [Other languages to be supported]() |
## Installation
### `✅ Recommended` Node, npm, and npx Development Environment
npx requires a Node environment. Please install Node first.
- ### `✅ Recommended` Node, npm, and npx Development Environment
> npx requires a Node environment. Please install Node first.
```sh

@@ -23,3 +23,3 @@ npx json2http build -l dart@3

### Flutter and Dart Development Environment
- ### Flutter and Dart Development Environment
```sh

@@ -30,11 +30,16 @@ dart pub add dev:json2http

### HarmonyOS Development Environment
Add the following configuration to oh-package.json5.
- ### HarmonyOS Development Environment
> _Due to the release restrictions of `OpenHarmony Third-party Library Center`, from version `v0.0.14`,
> independent executable files are no longer provided. Instead, JS scripts are provided and executed by `node`.
> Fortunately, `OpenHarmony Developer Tools` and `DevEco-Studio` come with `node`. Just add it to the `PATH` environment variable._
> - _`OpenHarmony Developer Tools`'s `node` is usually at: command-line-tools/tool/node/bin/node_
> - _`DevEco-Studio`'s `node` is usually at: DevEco-Studio.app/Contents/tools/node/bin/node_
>
> _The above methods are a bit cumbersome, so it is still recommended to use the npx method for simplicity._
> Add the following configuration to oh-package.json5.
```json5
{
"scripts": {
// Windows system
"json2http": "./oh_modules/json2class/src/main/resources/rawfile/json2http-win.exe build -l arkTs@12",
// macOS system
"json2http": "./oh_modules/json2class/src/main/resources/rawfile/json2http-macos build -l arkTs@12"
"json2http": "node ./oh_modules/json2http/src/main/resources/rawfile/json2http build -l arkTs@12",
}

@@ -50,13 +55,499 @@ }

## Quick Start
JSON files support both json and json5 formats.
```json5
// ~/projects/config/root.json
{
'/api/blood/index': {
title: 'Baby blood type calculation',
method: 'GET',
params: {
father: '',
mother: '',
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
```
筹备发布中
By default, it searches for and converts json configurations in the current directory where the command is executed.
```sh
cd ~/projects/config/
npx json2http build -l dart@3
```
In preparation for release
Usage of Code
```dart
import 'json2http.dart';
<!-- ohpm install json2http -->
main() async {
// Global configuration
Json2http.setPlan = (p) {
p.baseURL = 'https://qqlykm.cn';
p.process = (reply) {
final data = reply.data;
if (data is Map) {
if (data['code'] != 200) {
// Set error to determine whether to throw an exception
// Avoid checking status code for every interface
reply.error = data['msg'];
}
}
};
};
## JSON Configuration Guide
// Initiate request
final plan = await Json2http.single.apibloodindex((p) {
// Parameter configuration for a single request
p.params.father = 'A';
p.params.mother = 'O';
});
// Return value
print(plan.res.toJson());
print(plan.res.msg);
print(plan.res.data.possible.elementAtOrNull(0));
print(plan.res.data.impossible.elementAtOrNull(0));
}
```
## JSON Configuration
### A Complete HTTP Request Configuration
```json5
{
'/api/test/path': {
title: 'Short description of the interface',
method: 'POST',
headers: { 'x-some-key': '' },
params: {
args1: '', args2: '', args3: '...'
},
body: {
type: 'json',
data: {
args4: 0, args5: '', args6: true, args7: [''],
},
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
```
### /api/test/path
- Description: Configure the interface path address
- Validation: Must start with "/"
- Required: Yes
### title
- Description: Configure a short description of the interface
- Validation: Must be a string
- Required: Yes
### method
- Description: http method
- Validation: GET, POST, DELETE, PUT, etc.
- Required: Yes
### headers
- Description: http headers
- Validation: Must be map<string, string or Array<string>> type
- Required: No
### params
- Description: http path query parameters
- Validation: Must be map<string, string | number | boolean> type
- Required: No
- Example:
```json5
{
'/api/test/path': {
title: '',
method: 'GET',
params: { xxx: '', yyy: 123, zzz: true },
},
}
```
### body
- Description: http body configuration
- Validation: Can only configure type and data fields under it
- Required: No
### body.type
- Description: http body type
- Validation: json, map, form, plain, byte
- Required: Yes
### body.data [body.type = json]
- Validation: data can be any valid json data
- Required: Yes
- Example:
```json5
{
'/api/test/path1': {
title: '',
method: 'POST',
body: {
type: 'json',
data: 1,
},
},
'/api/test/path2': {
title: '',
method: 'POST',
body: {
type: 'json',
data: {
xxx: 0, yyy: [''], zzz: true
},
},
},
}
```
### body.data [body.type = map]
- Validation: data must be map<string, string | number | boolean> type
- Required: Yes
- Example:
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'map',
data: { xxx: '', yyy: 123, zzz: true },
},
},
}
```
### body.data [body.type = form]
- Validation:
1. data must contain fields and files fields
2. files field must be map<string, string | Array<string>> type
3. fields field must be map<string, (string | number | boolean) | Array<string | number | boolean>> type
- Required: Yes
- Example:
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'form',
data: {
fields: { xxx: '', yyy: 123 },
files: { aaa: [true], bbb: '' },
},
},
},
}
```
### body.data [body.type = plain]
- Validation: data must be a string
- Required: Yes
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'plain',
data: '',
},
},
}
```
### body.data [body.type = byte]
- Validation: data must be a string
- Required: Yes
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'byte',
data: '',
},
},
}
```
### res
- Description: http return value
- Validation: Any valid json object
- Required: No
- Example:
```json5
{
'/api/test/path': {
title: '',
method: 'GET',
res: {
statusCode: '',
statusMessage: '',
data: { xxx: '', yyy: 1 },
},
},
}
```
## Other Configurations
### Path Parameters
```json5
{
'/api/test/{user}': {
title: '',
method: 'GET',
},
}
```
```dart
main() async {
await Json2http.single.apitestuser((p) {
p.seg.user = 'user';
});
}
```
### Custom Method Name
If you really don't like using the interface address as the method name, you can also customize the method name in this way.
```json5
{
apiMethodName: {
path: '/api/test/path',
title: '',
method: 'GET',
params: { xxx: '' },
},
}
```
```dart
main() async {
await Json2http.single.apiMethodName((p) {
p.params.xxx = 'user';
});
}
```
### Rely on json2class to generate objects
- params
- body.data
- body.data.fields
- body.data.files
- res
The code generated for the above fields will inherit `json2class`, and the configuration and usage of the corresponding values follow [json2class](https://github.com/yangfanzn/json2any/blob/main/packages/json2class/README.md), such as:
- How to set fields as optional
- How to set filling methods
- The usage of the generated code is also completely consistent
### Referencing an Existing Structure
Similar to `json2class`, the configuration of `json2http` can also reference an existing structure through `{ $meta: { ref: '' } }`.
The difference is that when referencing a parent structure different from the current one, `json2class` uses the file as the basic unit, while `json2http` uses the `interface` as the basic unit.
In the following example, `res` in interface `/api/test/path2` can reuse `res` in `/api/test/path1`.
```json5
{
'/api/test/path1': {
title: '',
method: 'GET',
res: {
xxx: '',
},
},
'/api/test/path2': {
title: '',
method: 'GET',
res: {
$meta: { ref: '/api/test/path1#/res' },
},
},
}
```
If the reference occurs in the current interface, the current interface address can be omitted.
```json5
{
'/api/test/path': {
title: '',
method: 'GET',
params: { xxx: '' },
res: {
$meta: { ref: '#/params' },
},
},
}
```
By referencing its own parent, recursive types can be generated.
```json5
{
'/api/test/path': {
title: '',
method: 'GET',
res: {
someKey: '',
child: {
: { ref: '#/res' },
},
},
},
}
```
### Setting Parameters Optional
- body.data [body.type = json]
- body.data [body.type = byte]
- body.data [body.type = plain]
In these three cases, you can set `?` mark for data, and the generated code can set data to `null`.
```json5
{
'/api/test/path': {
title: '',
method: 'POST',
body: {
type: 'json',
'data?': 1,
},
},
}
```
```dart
main() async {
await Json2http.single.apitestpath((p) {
p.body.data = null;
});
}
```
## Usage of Generated Code
### Global Entry Json2http
- Json2http.setPlan
```dart
import 'json2http.dart';
// You can customize the Agent to fully control the request call behavior
class XAgent extends Agent {
Future<Reply> fetch(Plan plan) async {
// Can be implemented based on the generated code
plan.reply.data = {'statusCode': '0'};
plan.reply.code = 200;
return plan.reply;
}
body(Plan plan) {
// Can be implemented based on the generated code
return null;
}
}
main() {
Json2http.setPlan = (plan) {
// Global configuration
plan.baseURL = 'http://localhost:3000';
plan.process = (reply) {
final data = reply.data;
if (data is Map) {
if (data['statusCode'] != '0') {
// Determine whether the final request throws an exception
reply.error = data['statusMessage'];
}
}
};
plan.ready = () {
// Set the original configuration in the original request tool
plan.option = xxx;
// Set the original request tool
plan.session = xxx;
};
if (plan is testagentplan) {
plan.agent = XAgent();
}
if (plan.title.startsWith('amap:')) {
// Special interface special configuration
plan.baseURL = 'https://restapi.amap.com/v3';
plan.path = '${plan.path}?key=xxx';
plan.process = (reply) {
var data = reply.data;
if (data is Map) {
if (data['status'] != '1') {
reply.error = data['info'];
}
}
};
}
if (plan is refreshtestplan) {
plan.after = () async {
if (plan.reply.error == 'small token is error') {
// Implement token refresh
final x = await Json2http.single.refreshtoken((p) {
p.headers = {'access-token': 'bearer xxx'};
});
// After re-setting the token, retry the interface
plan.headers['access-token'] = x.res.data;
await plan.fetch();
}
};
}
};
}
```
- Initiate interface call
```json5
{
'/api/blood/index': {
title: 'Baby blood type calculation',
method: 'GET',
params: {
father: '',
mother: '',
},
res: {
code: 0,
msg: '',
data: {
possible: [''],
impossible: [''],
},
copyright: '',
},
},
}
```
```dart
main() async {
final result = await Json2http.single.apibloodindex((plan) {
plan.params.father = 'A';
plan.params.mother = 'B';
});
}
```
## Additional Command-Line Options

@@ -87,4 +578,3 @@ ### -l, --language: Specifies the target language for the build.

## Feedback & Improvement
Thank you for using this tool! In order to quickly improve and release **version 1.0.0**,
we would love to hear your feedback and suggestions.
Thank you for using this tool! we would love to hear your feedback and suggestions.
If you encounter any issues or have suggestions for improvement,

@@ -91,0 +581,0 @@ please feel free to provide feedback through the following channels:

@@ -5,15 +5,15 @@ # json2http

json2http 是一个命令行工具,它依赖 json2class 将指定的 JSON(5) 文件转换成 HTTP 请求代码
json2http 是一个命令行工具,它依赖 [json2class](https://github.com/yangfanzn/json2any/blob/main/packages/json2class/README.md) 将指定的 JSON(5) 文件转换成 HTTP 请求代码
## 支持的语言
### 已支持
| [arkTs@12](https://developer.huawei.com/consumer/cn/arkts/) | [dart@3](https://dart.dev/) | [typescript@5](https://www.typescriptlang.org/) |
| [dart@3](https://dart.dev/) | [arkTs@12](https://developer.huawei.com/consumer/cn/arkts/) | [typescript@5](https://www.typescriptlang.org/) | [kotlin@1.3](https://kotlinlang.org/) | [swift@5.7](https://developer.apple.com/swift/) |
### 将会支持
| [java](https://dev.java/) | [kotlin](https://kotlinlang.org/) | [swift](https://developer.apple.com/cn/swift/) | [其他语言陆续支持]() |
| [java](https://dev.java/) | [其他语言陆续支持]() |
## 安装
### `✅ 推荐` node npm npx 开发环境
npx 需要 node 环境,请先安装 node
- ### `✅ 推荐` node npm npx 开发环境
> npx 需要 node 环境,请先安装 node
```sh

@@ -23,3 +23,3 @@ npx json2http build -l dart@3

### Flutter、Dart 开发环境
- ### Flutter、Dart 开发环境
```sh

@@ -30,11 +30,15 @@ dart pub add dev:json2http

### 鸿蒙开发环境
将如下配置写入 oh-package.json5
- ### 鸿蒙开发环境
> _由于 `OpenHarmony三方库中心仓` 发布限制,从 `v0.0.14` 版本开始将无法再直接提供独立的可执行文件,改为提供 js 脚本,
> 由 `node` 解释执行。幸运的是,`OpenHarmony 开发者工具`和`DevEco-Studio`自带 `node`,将其设置到 `PATH` 环境变量中即可_
> - _`OpenHarmony 开发者工具` 中的 `node` 通常在如下路径:command-line-tools/tool/node/bin/node_
> - _`DevEco-Studio` 中的 `node` 通常在如下路径:DevEco-Studio.app/Contents/tools/node/bin/node_
>
> _以上方法略显麻烦,所以还是推荐您使用 `npx` 方式,简单快捷_
> 将如下配置写入 oh-package.json5
```json5
{
"scripts": {
// windows 系统
"json2http": "./oh_modules/json2class/src/main/resources/rawfile/json2http-win.exe build -l arkTs@12",
// macOS 系统
"json2http": "./oh_modules/json2class/src/main/resources/rawfile/json2http-macos build -l arkTs@12"
"json2http": "node ./oh_modules/json2http/src/main/resources/rawfile/json2http build -l arkTs@12",
}

@@ -158,3 +162,3 @@ }

- 作用:http method
- 校验:GET、POST、DELETE、PUT
- 校验:GET、POST、DELETE、PUT 等
- 必须:是

@@ -169,3 +173,3 @@

- 作用:http path 查询参数
- 校验:必须是 map<string, string> 类型
- 校验:必须是 map<string, string | number | boolean> 类型
- 必须:否

@@ -178,3 +182,3 @@ - 示例:

"method": "GET",
"params": { "xxx": "", "yyy": "" }
"params": { "xxx": "", "yyy": 123, "zzz": true }
}

@@ -222,3 +226,3 @@ }

### body.data [body.type = map]
- 校验:data 必须是 map<string, string> 类型
- 校验:data 必须是 map<string, string | number | boolean> 类型
- 必须:是

@@ -233,3 +237,3 @@ - 示例:

"type": "map",
"data": { "xxx": "", "yyy": "" }
"data": { "xxx": "", "yyy": 123, "zzz": true }
}

@@ -243,3 +247,4 @@ }

1. data 下必须存在 fields 和 files 字段
2. fields 和 files 字段必须是 map<string, string 或 Array<string>> 类型
2. files 字段必须是 map<string, string | Array<string>> 类型
3. fields 字段必须是 map<string, (string | number | boolean) | Array<string | number | boolean>> 类型
- 必须:是

@@ -255,4 +260,4 @@ - 示例:

"data": {
"fields": { "xxx": "", "yyy": "" },
"files": { "aaa": [""], "bbb": "" },
"fields": { "xxx": "", "yyy": 123 },
"files": { "aaa": [true], "bbb": "" },
}

@@ -296,3 +301,2 @@ }

### res

@@ -449,2 +453,109 @@ - 作用:http 返回值

### 全局入口 Json2http
- Json2http.setPlan
```dart
import 'json2http.dart';
// 可以自定义 Agent,完全控制请求调用行为
class XAgent extends Agent {
Future<Reply> fetch(Plan plan) async {
// 可以参照已经生成的代码中进行实现
plan.reply.data = {'statusCode': '0'};
plan.reply.code = 200;
return plan.reply;
}
body(Plan plan) {
// 可以参照已经生成的代码中进行实现
return null;
}
}
main() {
Json2http.setPlan = (plan) {
// 全局配置
plan.baseURL = 'http://localhost:3000';
plan.process = (reply) {
final data = reply.data;
if (data is Map) {
if (data['statusCode'] != '0') {
// 决定最终请求是否抛出异常
reply.error = data['statusMessage'];
}
}
};
plan.ready = () {
// 设置原始请求工具中的原始配置
plan.option = xxx;
// 设置原始请求工具
plan.session = xxx;
};
if (plan is testagentplan) {
plan.agent = XAgent();
}
if (plan.title.startsWith('amap:')) {
// 特殊接口特殊配置
plan.baseURL = 'https://restapi.amap.com/v3';
plan.path = '${plan.path}?key=xxx';
plan.process = (reply) {
var data = reply.data;
if (data is Map) {
if (data['status'] != '1') {
reply.error = data['info'];
}
}
};
}
if (plan is refreshtestplan) {
plan.after = () async {
if (plan.reply.error == 'small token is error') {
// 实现 token 刷新
final x = await Json2http.single.refreshtoken((p) {
p.headers = {'access-token': 'bearer xxx'};
});
// 重新设置 token 后,接口重试
plan.headers['access-token'] = x.res.data;
await plan.fetch();
}
};
}
};
}
```
- 发起接口调用
```json5
{
"/api/blood/index": {
"title": "宝宝血型计算",
"method": "GET",
"params": {
"father": "",
"mother": ""
},
"res": {
"code": 0,
"msg": "",
"data": {
"possible": [""],
"impossible": [""]
},
"copyright": ""
}
}
}
```
```dart
main() async {
final result = await Json2http.single.apibloodindex((plan) {
plan.params.father = 'A';
plan.params.mother = 'B';
});
}
```
## 命令行其他选项

@@ -475,3 +586,3 @@ ### -l --language,指定需要构建的语言

## 反馈与改进
感谢您使用本工具,为了尽快完善并发布 **1.0.0 正式版本**,我们希望听到您的意见和建议。
感谢您使用本工具,我们希望听到您的意见和建议。
如果您在使用过程中遇到问题,或者有任何改进的建议,欢迎通过如下方式反馈:

@@ -478,0 +589,0 @@

export * as Json2httpBin from './src/bin';
export * as Json2HttpBase from './src/base';
export * as Json2HttpDart from './src/dart';
export * as Json2HttpArkTs from './src/arkTs';
export * as Json2HttpKotlin from './src/kotlin';
export * as Json2HttpSwift from './src/swift';
export * from 'json2class';
export * as Json2HttpBase from './src/base';
export * as Json2HttpDart from './src/dart';
export * as Json2HttpArkTs from './src/arkTs';
export * as Json2HttpKotlin from './src/kotlin';
export * as Json2HttpSwift from './src/swift';
export { json2http as default } from './src';

@@ -5,5 +5,4 @@ import { Json2classBase } from 'json2class';

core2http(http: typeof Http<Json2classBase.Complex, Json2classBase.Simple<Json2classBase.Complex>>, complex: typeof Json2classBase.Complex, simple: typeof Json2classBase.Simple<Json2classBase.Complex>, key: string, json: Record<string, any>, file?: string): Http<Json2classBase.Complex, Json2classBase.Simple<Json2classBase.Complex>>;
convertLaunch(str: string): string;
isBodyFiles(self: Json2classBase.Key): any;
}
export declare const func: Func;
export declare enum Language {
Dart3 = "dart@3",
ArkTs12 = "arkTs@12",
Typescript5 = "typescript@5"
Typescript5 = "typescript@5",
Kotlin1_3 = "kotlin@1.3",
Swift5_7 = "swift@5.7"
}

@@ -10,3 +12,5 @@ export declare enum DefaultAgent {

Typescript_Axios1 = "typescript_axios@1",
Typescript_Fetch0 = "typescript_fetch@0"
Typescript_Fetch0 = "typescript_fetch@0",
Kotlin_OkHttp4 = "kotlin_okhttp@4",
Swift_Alamofire5 = "swift_alamofire@5"
}

@@ -18,5 +22,6 @@ export declare const env: {

language: Language;
defaultAgent: DefaultAgent;
search: string;
output: string;
library: string;
defaultAgent: DefaultAgent;
};

@@ -23,0 +28,0 @@ export declare const code2message: {

# json2http
## 0.0.1
- init
## 0.0.2
- 2025-03-09 test
## 0.0.3
- 2025-05-03 Fixed issue with repeated calls to response.text() when the backend directly returns a stream
- 2025-05-03 Added ability to quickly view the path from the entry method
- 2025-05-03 Added a configurable placeholder item via ref [the current res meets this need]
- 2025-05-04 Removed null values when sending data to the cloud

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet