A tool that generates several Moya providers and models from given config files. You can check the example project here
- Add
pod 'moya-generator', :podspec => 'https://raw.githubusercontent.com/frkncngz/moya-generator/master/moya-generator.podspec'
to your Podfile andpod install
- In Xcode: Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left.
- Paste the following script:
"${PODS_ROOT}"/moya-generator/moya-generator --inputPath "$SRCROOT"/provider-configs/ --outputPath "$SRCROOT"/"$PROJECT_NAME"
- Drag the new
Run Script
phase above theCompile Sources
. - Build your project, in Finder you will now see a new
Providers
folder in the$SRCROOT/$PROJECT_NAME
-folder, drag the theProviders
folder into your project and uncheckCopy items if needed
. - Profit.
- Create a
provider-configs
folder in your project's root directory - Place your json configurations in that folder
- Build your project and you will see a newly created
Providers
folder with the generated providers and models.
-
providerName
: Name of the provider -
custom
: (Optional) If you set this totrue
, the Provider and Models will be created only once (if they are not present). If they are created before, the generator will not overwrite them so you can modify them and create custom providers. If you set this, don't forget to commit the file to your repository. -
baseURL
: Base URL of the API -
headers
: Default headers -
endpoints
: An array that defines the endpointsname
: Name of the endpoint (enum)altName
: (Optional) Alternative name which can be used in parameterMapping.path
: Path of the endpoint from baseURLmethod
: Method of the endpoint (post, get, put etc)task
: This can bedata
,plain
andparameters
.plain
anddata
are harcoded and generatesreturn .requestPlain
andreturn Task.requestData(data)
respectively. If you set this toparameters
, it will generate a request with these parametersparameters
: An array that defines the parametersname
: Name of the parametertype
: Type of the parameter (String, Bool etc)outputName
: (Optional) If you want to change the name of the parameter while building the request, you should set it herefixedValue
: (Optional) If you want to set a fixed value to the parameter, you can set it here. If you set this, the parameter will be discarded from other places
parameterMapping
: (Optional) If you want to interfere with the parameters before sending the request, this is the place to do it. This dictionary will be your final parameters. Currently it only supports$parameters
and$altName
keys.$parameters
will get all the parameters from the request.
If you set this:
{ "params": "[$parameters]", "method": "$altName" }
Your final request parameters will look like this:
{ "method": "alt name that is set in the json", "params": [ [ "param1": "value1", "param2": "value2", "param3": "value3" ] ] }
-
models
: An array that defines the Codable models-
name
: Name of the model -
equatable
: (Optional) If you set this to true, it will add Equatable protocol next to Codable. -
parameters
: An array that defines the variablesname
: Name of the variabletype
: Type of the variableoutputName
: (Optional) If you set this, the generator will add CodingKeys enum corresponding to this variable.
{ "providerName":"ExampleRPC", "custom":true, "baseURL":"https://example-rpc.trustwalletapp.com", "headers":{ "content-type":"application/json", "accept":"application/json" }, "endpoints":[ { "name":"account", "altName":"account_info", "path":"/", "method":"post", "task":"parameters", "parameters":[ { "name":"address", "outputName":"account", "type":"String" }, { "name":"strict", "type":"Bool", "fixedValue":"true" }, { "name":"ledger_index", "type":"String", "fixedValue":"\"current\"" }, { "name":"queue", "type":"Bool", "fixedValuefixedValue":"true" } ], "parameterMapping":{ "params":"[$parameters]", "method":"$altName" } }, { "name":"broadcast", "path":"/transactions/broadcast", "method":"post", "task":"data", "parameters":[ { "name":"data", "type":"Data" } ] }, { "name":"transaction", "path":"transactions/info/{hash}", "method":"get", "task":"plain", "parameters":[ { "name":"hash", "type":"String" } ] } ], "models":[ { "name":"RippleAccount", "parameters":[ { "name":"data", "outputName":"account_data", "type":"RippleAccountData" } ] }, { "name":"RippleAccountData", "parameters":[ { "name":"Balance", "type":"String" }, { "name":"Sequence", "type":"Int64" } ] } ] }
-
- Currently it doesn't support different names for the same input and parameter.
- It doesn't support custom headers for different endpoints.