Skip to content

Commit

Permalink
[CAT-1241][Node] Add FileTransfer object to allow in-memory only file…
Browse files Browse the repository at this point in the history
… upload/download (#91)
  • Loading branch information
dvacca-onfido authored Jun 20, 2024
1 parent c68d020 commit e92d340
Show file tree
Hide file tree
Showing 9 changed files with 573 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/update-specs-and-client-libraries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ jobs:
- generator: typescript-axios
git_repo_id: onfido-node
preCommit: |
sed -iE 's/\([ <{]\)File\([>},]\)/\1FileTransfer\2/g' api.ts
npx prettier --write package.json
npm install
version: ${{ needs.generate_specs_and_libraries.outputs.typescript_axios_version }}
Expand Down Expand Up @@ -214,7 +215,7 @@ jobs:
- name: Integrate generated code (${{ matrix.version }})
if: ${{ matrix.update }}
run: |
rsync -r --delete-after --exclude='/.git*' --exclude='/CHANGELOG*' \
rsync -r --delete-after --exclude='/.git*' --exclude='/CHANGELOG*' --exclude='/.release.json' \
--exclude='/.openapi-generator-ignore' --exclude='/.openapi-generator/FILES' \
--exclude-from=generators/${{ matrix.generator }}/exclusions.txt \
generated/artifacts/${{ matrix.generator }}/ .
Expand Down
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ To update the OpenAPI generator version, bump the version in both [update-specs-
SHA256SUM for template ... changed, diff reported below. To overwrite template, run:
...
```

This happens when templates we're overriding have been updated. The script automatically fixes checksums for one generator at each run, but the templates need to be carefully reviewed and updated by following the procedure below:

1. Check which files have changed, by running `git diff generators/**/templates/SHA256SUM`
2. Compare each file with the one that has been freshly generated, e.g. if _libraries/okhttp-gson/ApiClient.mustache_ checksum has been denoted as modified for _java/okhttp-gson_ generator:
```sh
diff generators/java/okhttp-gson/templates/libraries/okhttp-gson/ApiClient.mustache generated/templates/java/okhttp-gson/libraries/okhttp-gson/ApiClient.mustache
```
```sh
diff generators/java/okhttp-gson/templates/libraries/okhttp-gson/ApiClient.mustache generated/templates/java/okhttp-gson/libraries/okhttp-gson/ApiClient.mustache
```
3. Add all changes from the new version except the ones noted by mustache comments (i.e. `{{! }}`)
4. Commit changes to both templates and SHA256SUM files

Expand All @@ -111,23 +112,23 @@ The changes to README.md should be carefully reviewed by comparing `generated/te

Repository is open to external contributions. At this end please:

1. [Fork](<https://github.com/onfido/onfido-openapi-spec/fork>) repository
1. [Fork](https://github.com/onfido/onfido-openapi-spec/fork) repository
2. Create your feature branch (`git checkout -b my-new-feature`)
4. Make your changes, see below sections for project setup and testing.
4. To update one (or more) client libraries, clone them in the parent folder so that all the _onfido-*_ repositories lie at the same level. Then run the script `./shell/generate.sh` in the _onfido-openapi-spec_ folder and `./shell/sync-lib.sh` in each of the client libraries' folder, as in the examples below:
3. Make your changes, see below sections for project setup and testing.
4. To update one (or more) client libraries, clone them in the parent folder so that all the _onfido-\*_ repositories lie at the same level. Then run the script `./shell/generate.sh` in the _onfido-openapi-spec_ folder and `./shell/sync-lib.sh` in each of the client libraries' folder, as in the examples below:

```sh
../onfido-openapi-spec/shell/sync-lib.sh java java/okhttp-gson
../onfido-openapi-spec/shell/sync-lib.sh php
```
```sh
../onfido-openapi-spec/shell/sync-lib.sh java java/okhttp-gson
../onfido-openapi-spec/shell/sync-lib.sh php
```

5. To verify changes to the OpenAPI definition with external tools, run:

```sh
./shell/refresh-openapi-spec-for-documentation.py \
generated/artifacts/openapi/openapi.json \
generated/artifacts/openapi-documentation/openapi.json
```
```sh
./shell/refresh-openapi-spec-for-documentation.py \
generated/artifacts/openapi/openapi.json \
generated/artifacts/openapi-documentation/openapi.json
```

6. Before committing your changes, run the script `./shell/run-prettier.sh`
7. Commit your changes (`git commit -am 'Add some feature'`)
Expand All @@ -142,12 +143,12 @@ Described below is the procedure on how to deliver new client libraries:
2. Manually trigger an [update-specs-and-client-libraries](https://github.com/onfido/onfido-openapi-spec/actions/workflows/update-specs-and-client-libraries.yaml) by clicking over the _Run workflow_ button
3. Select the type of change:

| Type | Description |
|-------------|-----------------------------------------------------------------------------------------------------------------------|
| _No change_ | no change expected to client library code |
| _Patch_ | bug fix not causing any change to client library interface |
| _Minor_ | backward compatible change to client library interface (e.g. new endpoint, new optional parameters) |
| _Major_ | non-backward compatible change to client library interface (e.g. remove or change endpoint, new mandatory parameters) |
| Type | Description |
| ----------- | --------------------------------------------------------------------------------------------------------------------- |
| _No change_ | no change expected to client library code |
| _Patch_ | bug fix not causing any change to client library interface |
| _Minor_ | backward compatible change to client library interface (e.g. new endpoint, new optional parameters) |
| _Major_ | non-backward compatible change to client library interface (e.g. remove or change endpoint, new mandatory parameters) |

4. Select the libraries to update
5. A new PR will be created for each library (or overridden if already present)
Expand Down
5 changes: 5 additions & 0 deletions generators/typescript-axios/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ enumUnknownDefaultCase: true
supportsES6: true
withNodeImports: true
httpUserAgent: onfido-node/${CLIENT_LIBRARY_VERSION}
# Type mapping doesn't work with this generator
# typeMappings:
# File: FileTransfer
files:
LICENSE.mustache:
destinationFilename: LICENSE
webhook-event-verifier.mustache:
destinationFilename: webhook-event-verifier.ts
file-transfer.mustache:
destinationFilename: file-transfer.ts
2 changes: 2 additions & 0 deletions generators/typescript-axios/templates/SHA256SUM
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

e13ee7c0cad9a79bab00e8b2a7942bc5a78f63115ece3dfd71a6472bdb02dd22 ./README.mustache
3a077701e62c4d0942657c78cf0116cbe58436e34d723d81aaed7ed0d56d2ed1 ./api.mustache
0f4ea3b4db29a3bbada4c5ad5e4b08fa598305ba09aa29d237edee1634f8805b ./apiInner.mustache
d8d68213e1a9a9983f89f688aaf31360f69d34a871cab4fc94f59a40279b13d9 ./configuration.mustache
5030fcd1954b4d981f2d06fce4900fe29c97c7b74ee66f2eb5f45e81b9252a94 ./index.mustache
28f5e210e99474200c8e5f13e4f2374c1556406eb71c15e808d81913000cd549 ./package.mustache
Expand Down
60 changes: 60 additions & 0 deletions generators/typescript-axios/templates/api.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* tslint:disable */
/* eslint-disable */
{{>licenseInfo}}

{{^withSeparateModelsAndApi}}
import type { Configuration } from './configuration';
import type { AxiosPromise, AxiosInstance, RawAxiosRequestConfig } from 'axios';
import globalAxios from 'axios';
{{#withNodeImports}}
// URLSearchParams not necessarily used
// @ts-ignore
import { URL, URLSearchParams } from 'url';
{{#multipartFormData}}
import FormData from 'form-data'
{{/multipartFormData}}
{{/withNodeImports}}
// Some imports not used depending on template conditions
// @ts-ignore
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from './common';
import type { RequestArgs } from './base';
// @ts-ignore
import { BASE_PATH, COLLECTION_FORMATS, BaseAPI, RequiredError, operationServerMap } from './base';

{{! ONFIDO: Added lines of code - BEGIN }}
import { FileTransfer } from './file-transfer';
import { AxiosHeaders } from 'axios';

globalAxios.interceptors.response.use(async (response) => {
if (response.headers instanceof AxiosHeaders && response.headers['content-type']) {
if ( ! response.headers['content-type'].toString().includes('application/json') ) {
const contentDisposition = response.headers['content-disposition'];
var filename = "";
if (contentDisposition && contentDisposition != "") {
const matcher = contentDisposition.match(/filename=['\"]?([^'\"\s]+)['\"]?/);
if (matcher != null) {
filename = matcher[1].replace(/.*[/\\\\]/g, "");
}
}
response.data = new FileTransfer(response.data, filename);
}
}
return response;
});
{{! ONFIDO: Added lines of code - END }}
{{#models}}
{{#model}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^isEnum}}{{^oneOf}}{{>modelGeneric}}{{/oneOf}}{{/isEnum}}{{/model}}
{{/models}}
{{#apiInfo}}{{#apis}}
{{>apiInner}}
{{/apis}}{{/apiInfo}}
{{/withSeparateModelsAndApi}}{{#withSeparateModelsAndApi}}
{{#apiInfo}}{{#apis}}{{#operations}}export * from './{{tsApiPackage}}/{{classFilename}}';
{{/operations}}{{/apis}}{{/apiInfo}}
{{/withSeparateModelsAndApi}}
Loading

0 comments on commit e92d340

Please sign in to comment.