github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/typescript-fetch/README.mustache (about) 1 ## {{npmName}}@{{npmVersion}} 2 3 Phrase Strings is a translation management platform for software projects. You can collaborate on language file translation with your team or order translations through our platform. The API allows you to import locale files, download locale files, tag keys or interact in other ways with the localization data stored in Phrase Strings for your account. 4 5 ## This SDK is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project: 6 7 - API version: {{appVersion}} 8 - Package version: {{npmVersion}} 9 {{^hideGenerationTimestamp}} 10 - Build date: {{generatedDate}} 11 {{/hideGenerationTimestamp}} 12 - Build package: {{npmName}} 13 {{#infoUrl}} 14 For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}) 15 {{/infoUrl}} 16 17 18 This generator creates TypeScript/JavaScript client that utilizes [Fetch API](https://fetch.spec.whatwg.org/). The generated Node module can be used in the following environments: 19 20 Environment 21 * Node.js 22 * Webpack 23 * Browserify 24 25 Language level 26 * ES5 - you must have a Promises/A+ library installed 27 * ES6 28 29 Module system 30 * CommonJS 31 * ES6 module system 32 33 It can be used in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html)) 34 35 ### Building 36 37 To build and compile the typescript sources to javascript use: 38 ``` 39 npm install 40 npm run build 41 ``` 42 43 ### Publishing 44 45 First build the package then run ```npm publish``` 46 47 ### Installation 48 49 navigate to the folder of your consuming project and run one of the following commands. 50 51 _published:_ 52 53 54 ``` 55 npm install {{npmName}}@{{npmVersion}} --save 56 ``` 57 58 if you are using yarn 59 60 61 ``` 62 yarn install {{npmName}}@{{npmVersion}} 63 ``` 64 65 _unPublished (not recommended):_ 66 67 ``` 68 npm install PATH_TO_GENERATED_PACKAGE --save 69 ``` 70 71 72 ## Getting Started 73 74 Please follow the [installation](#installation) procedure and then run the following code: 75 76 ```javascript 77 import {Configuration, SpacesApi} from "{{npmName}}" 78 79 const configuration = new Configuration({apiKey: 'token API_TOKEN'}) 80 81 const spaceApi = new SpacesApi(configuration) 82 83 let requestParameters = { 84 accountId: 'ACCOUNT_ID' 85 } 86 87 spaceApi.spacesList(requestParameters).then(function (data) { console.log(data) }) 88 ``` 89 90 ## Getting Started in Node.js 91 92 This package is using `fetch` and `FormData` so make sure to have polyfills for it 93 94 here is the example package.json 95 96 ```json 97 { 98 "name": "openapi_typescript_test", 99 "version": "1.0.0", 100 "description": "", 101 "main": "dist/index.js", 102 "scripts": { 103 "start": "tsc && node dist/index.js", 104 "test": "echo \"Error: no test specified\" && exit 1" 105 }, 106 "author": "", 107 "license": "ISC", 108 "dependencies": { 109 "@types/node": "^14.0.1", 110 "form-data": "^3.0.0", 111 "node-fetch": "^2.6.0", 112 "phrase-js": "^{{npmVersion}}" 113 }, 114 "devDependencies": { 115 "tslint": "^5.20.1", 116 "typescript": "^3.9.2" 117 } 118 } 119 ``` 120 121 and you can use it as follows 122 123 ```javascript 124 import {Configuration, SpacesApi} from "phrase-js" 125 var FormData = require("form-data") 126 var fetch = require("node-fetch") 127 128 const globalAny: any = global; 129 globalAny.FormData = FormData 130 131 const configuration = new Configuration({ 132 apiKey: 'token API_TOKEN', 133 fetchApi: fetch 134 }) 135 136 const spaceApi = new SpacesApi(configuration) 137 138 let requestParameters = { 139 accountId: 'YOUR_ACCOUNT_ID' 140 } 141 142 spaceApi.spacesList(requestParameters).then(function (data) {console.log(data)}) 143 ``` 144 145 ### Upload 146 147 ```javascript 148 const uploadsApi = new UploadsApi(configuration) 149 const file = fs.createReadStream('example.json') 150 151 const requestParameters = { 152 projectId: 'YOUR_PROJECT_ID', 153 localeId: 'YOUR_LOCALE_ID', 154 file: file, 155 fileFormat: 'json' 156 } 157 158 uploadsApi.uploadCreate(requestParameters).then(function (data) { console.log(data) }) 159 ``` 160 161 In Node.js >= 18 there is `fetch` built-in, but you need to prepare the arguments slightly differently: 162 163 ```javascript 164 const fs = require("fs"); 165 const { File } = require('buffer'); 166 167 const buf = await fs.readFileSync(filePath); 168 const file = new File([buf], filePath); 169 170 const requestParams = { 171 projectId: 'YOUR_PROJECT_ID', 172 localeId: 'YOUR_LOCALE_ID', 173 file: file, 174 fileFormat: 'json' 175 }; 176 ``` 177 178 ## Datacenters 179 180 The API is only accessible via HTTPS and the current version is <code>v2</code>, which results in a base URL like: <code>{{{basePath}}}</code> depending on the datacenter. 181 182 ### EU Datacenter 183 ``` 184 {{{basePath}}} 185 ``` 186 187 This is the default datacenter. 188 189 ### US Datacenter 190 ``` 191 https://api.us.app.phrase.com/v2/ 192 ``` 193 194 ### Specifying US Datacenter 195 You can use the US datacenter by setting the following: 196 ``` 197 const configuration = new Configuration({basePath: 'https://api.us.app.phrase.com/v2/'}) 198 ``` 199 200 ## API Endpoints 201 202 All URIs are relative to *{{basePath}}* 203 204 Class | Method | HTTP request | Description 205 ------------ | ------------- | ------------- | ------------- 206 {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | **{{operationId}}** | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}} 207 {{/operation}}{{/operations}}{{/apis}}{{/apiInfo}} 208 209 ## Author 210 211 {{#apiInfo}}{{#apis}}{{#-last}}{{infoEmail}} 212 {{/-last}}{{/apis}}{{/apiInfo}} 213 214 ## Get help / support 215 216 Please contact [support@phrase.com](mailto:support@phrase.com?subject=[GitHub]%20phrase-js) and we can take more direct action toward finding a solution.