Skip to content

Commit

Permalink
feat: support send email with proxy type: internet (SAP#2641)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjtang1985 authored Jul 29, 2022
1 parent ec3c27d commit 05d82aa
Show file tree
Hide file tree
Showing 24 changed files with 770 additions and 65 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"workspaces": [
"packages/connectivity",
"packages/http-client",
"packages/mail-client",
"packages/openapi",
"packages/odata-common",
"packages/odata-v2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { DestinationOrFetchOptions, sanitizeDestination } from './destination';
import { ErrorWithCause } from '@sap-cloud-sdk/util';
import {
DestinationOrFetchOptions,
sanitizeDestination,
toDestinationNameUrl
} from './destination';
import { Destination } from './destination-service-types';
import { searchEnvVariablesForDestination } from './destination-from-env';
import {
Expand Down Expand Up @@ -32,6 +37,33 @@ export async function useOrFetchDestination(
: sanitizeDestination(destination);
}

/**
* Resolve a destination by the following steps:
* 1. Call [[useOrFetchDestination]]
* 2. Throw an error, when the resulting destination from the previous step is falsy
* 3. Return the checked destination.
* @param destination - A destination or the necessary parameters to fetch one.
* @returns A promise resolving to the requested destination on success.
* @internal
*/
export async function resolveDestination(
destination: DestinationOrFetchOptions
): Promise<Destination> {
const resolvedDestination = await useOrFetchDestination(destination).catch(
error => {
throw new ErrorWithCause('Failed to load destination.', error);
}
);
if (!resolvedDestination) {
throw Error(
`Failed to resolve the destination '${toDestinationNameUrl(
destination
)}'.`
);
}
return resolvedDestination;
}

/**
* Builds a destination from one of three sources (in the given order):
* - from the environment variable "destinations".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface Destination {
tokenServicePassword?: string;

/**
* The type of the destination, defaults to 'HTTP'. The SAP Cloud SDK only understands destinations of type 'HTTP'.
* The type of the destination, defaults to 'HTTP'. The SAP Cloud SDK only understands destinations of type 'HTTP' and 'MAIL'.
*/
type?: 'HTTP' | 'LDAP' | 'MAIL' | 'RFC';

Expand Down
32 changes: 11 additions & 21 deletions packages/http-client/src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
Destination,
DestinationOrFetchOptions,
getAgentConfig,
toDestinationNameUrl,
useOrFetchDestination
toDestinationNameUrl
} from '@sap-cloud-sdk/connectivity';
import {
resolveDestination,
defaultResilienceBTPServices,
DestinationConfiguration,
getAdditionalHeaders,
Expand Down Expand Up @@ -55,13 +55,14 @@ export async function buildHttpRequest(
destination: DestinationOrFetchOptions
): Promise<DestinationHttpRequestConfig> {
const resolvedDestination = await resolveDestination(destination);
if (!resolvedDestination) {
if (!!resolvedDestination.type && resolvedDestination.type !== 'HTTP') {
throw Error(
`Failed to resolve the destination '${toDestinationNameUrl(
`The type of the destination '${toDestinationNameUrl(
destination
)}'.`
)}' has to be 'HTTP', but is '${destination.type}'.`
);
}

const headers = await buildHeaders(resolvedDestination);

return buildDestinationHttpRequestConfig(resolvedDestination, headers);
Expand Down Expand Up @@ -103,16 +104,15 @@ export function execute<ReturnT>(executeFn: ExecuteHttpRequestFn<ReturnT>) {
options?: HttpRequestOptions
): Promise<ReturnT> {
const resolvedDestination = await resolveDestination(destination);
if (!resolvedDestination) {
if (!!resolvedDestination.type && resolvedDestination.type !== 'HTTP') {
throw Error(
`Failed to resolve the destination '${toDestinationNameUrl(
`The type of the destination '${toDestinationNameUrl(
destination
)}'.`
)}' has to be 'HTTP', but is '${destination.type}'.`
);
}
const destinationRequestConfig = await buildHttpRequest(
resolvedDestination
);

const destinationRequestConfig = await buildHttpRequest(destination);
logCustomHeadersWarning(requestConfig.headers);
const request = await buildRequestWithMergedHeadersAndQueryParameters(
requestConfig,
Expand Down Expand Up @@ -423,16 +423,6 @@ async function buildHeaders(
}
}

async function resolveDestination(
destination: DestinationOrFetchOptions
): Promise<Destination | null> {
try {
return await useOrFetchDestination(destination);
} catch (error) {
throw new ErrorWithCause('Failed to load destination.', error);
}
}

function merge<T extends HttpRequestConfig>(
destinationRequestConfig: DestinationHttpRequestConfig,
customRequestConfig: T
Expand Down
57 changes: 57 additions & 0 deletions packages/mail-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!-- sap-cloud-sdk-logo -->
<!-- This block is inserted by scripts/replace-common-readme.ts. Do not adjust it manually. -->
<a href="https://sap.com/s4sdk"><img src="https://help.sap.com/doc/2324e9c3b28748a4ae2ad08166d77675/1.0/en-US/logo-with-js.svg" alt="SAP Cloud SDK for JavaScript Logo" height="122.92" width="226.773"/></a>
<!-- sap-cloud-sdk-logo-stop -->

# @sap-cloud-sdk/mail-client (experimental)
With the SAP Cloud SDK mail client you can leverage the connectivity and destination services on SAP BTP to send e-mails from your application.

## This package is experimental
The API of this package is not stable and will likely change in future versions.

## Installation

```
$ npm install @sap-cloud-sdk/mail-client
```

## Usage

Use the `sendMail` function to send e-mails.

```ts
`sendMail`(destination, { from: '[email protected]', to: '[email protected]', subject: 'subject', text: 'text' });
```

<!-- sap-cloud-sdk-common-readme -->
<!-- This block is inserted by scripts/replace-common-readme.ts. Do not adjust it manually. -->
## Support

The recommended way to get in touch with us is to create an issue on [GitHub](https://github.com/SAP/cloud-sdk-js/issues).
Select the issue category `Bug`, `Feature` or `Question` depending on the nature of your request.
We try to provide fixes, features and answers as soon as possible.

## Contribute

If you would like to contribute to the SAP Cloud SDK, please make yourself familiar with our [contributing guidelines](https://github.com/SAP/cloud-sdk-js/blob/main/CONTRIBUTING.md) and follow the given instructions.

## Links

- [Github](https://github.com/SAP/cloud-sdk-js)
- [Github - Releases](https://github.com/SAP/cloud-sdk-js/releases)

<br>

- [SAP Cloud SDK Documentation portal](https://sap.github.io/cloud-sdk/)
- [SAP Cloud SDK Documentation portal - Getting started guide](https://sap.github.io/cloud-sdk/docs/js/getting-started)
- [SAP Cloud SDK Documentation portal - API documentation](https://sap.github.io/cloud-sdk/docs/js/api-reference-js-ts)

<br>

- [developers.sap.com - Product Overview](https://developers.sap.com/topics/cloud-sdk.html)
- [developers.sap.com - Tutorials](https://developers.sap.com/tutorial-navigator.html?tag=products:technology-platform/sap-cloud-sdk/sap-cloud-sdk&tag=topic:javascript)

## License

The SAP Cloud SDK is released under the [Apache License Version 2.0.](http://www.apache.org/licenses/)
<!-- sap-cloud-sdk-common-readme-stop -->
3 changes: 3 additions & 0 deletions packages/mail-client/internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// eslint-disable-next-line import/no-internal-modules
export * from './dist/internal';
// # sourceMappingURL=internal.d.ts.map
11 changes: 11 additions & 0 deletions packages/mail-client/internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
function __export(m) {
for (const p in m) {
if (!exports.hasOwnProperty(p)) {
exports[p] = m[p];
}
}
}
Object.defineProperty(exports, '__esModule', { value: true });
__export(require('./dist/internal'));
// # sourceMappingURL=internal.js.map
6 changes: 6 additions & 0 deletions packages/mail-client/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// eslint-disable-next-line
const commonConfig = require('../../test-resources/jest.common.config');
module.exports = {
...commonConfig,
displayName: 'mail-client'
};
49 changes: 49 additions & 0 deletions packages/mail-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@sap-cloud-sdk/mail-client",
"version": "2.6.0",
"description": "SAP Cloud SDK for JavaScript e-mail client",
"homepage": "https://sap.github.io/cloud-sdk/docs/js/overview-cloud-sdk-for-javascript",
"license": "Apache-2.0",
"keywords": [
"sap-cloud-sdk",
"cloud-sdk",
"sap-cloud-platform",
"mail",
"client"
],
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"publishConfig": {
"access": "public"
},
"files": [
"dist/**/*.js",
"dist/**/*.js.map",
"dist/**/*.d.ts",
"dist/**/*.d.ts.map",
"internal.js",
"internal.d.ts"
],
"repository": "github:SAP/cloud-sdk-js",
"scripts": {
"compile": "tsc -b",
"prepublishOnly": "yarn compile && yarn readme",
"test": "yarn test:unit",
"test:unit": "jest",
"coverage": "jest --coverage",
"lint": "eslint --ext .ts . && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c",
"lint:fix": "set TIMING=1 && eslint --ext .ts . --fix --quiet && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --loglevel error",
"check:dependencies": "depcheck .",
"check:public-api": "ts-node ../../scripts/check-public-api-cli.ts",
"readme": "ts-node ../../scripts/replace-common-readme.ts"
},
"dependencies": {
"@sap-cloud-sdk/connectivity": "^2.5.0",
"@sap-cloud-sdk/util": "^2.5.0",
"nodemailer": "^6.7.6"
},
"devDependencies": {
"@types/nodemailer": "^6.4.4",
"typescript": "~4.7.4"
}
}
11 changes: 11 additions & 0 deletions packages/mail-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { sendMail } from './mail-client';
export type {
MailOptions,
Address,
Attachment,
AttachmentLike,
Headers,
Envelope,
MailResponse,
MailDestination
} from './mail-client-types';
2 changes: 2 additions & 0 deletions packages/mail-client/src/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './mail-client';
export * from './mail-client-types';
Loading

0 comments on commit 05d82aa

Please sign in to comment.