FEATURE: update and delete function

This commit is contained in:
Mark Silva 2023-05-02 17:47:45 +08:00
parent 2d4a47867a
commit 92a7f805a2
11 changed files with 381 additions and 51 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/node_modules
/dist
yarn-error.log
.DS_Store

View File

@ -1,20 +1,17 @@
# OBP-TypeScript
## Usage
#### Install
#### Symlink
Checkout the obp-typescript library from https://github.com/OpenBankProject/OBP-TypeScript.
Inside the obp-sdk repository folder, execute the **yarn link** command.
##### yarn
```
yarn link
yarn add obp-typescript
```
To link the **obp-typescript** library into your app, run the command inside your app.
##### npm
```
yarn link obp-typescript
npm install obp-typescript
```
#### Example

View File

@ -4,16 +4,22 @@ import {
DirectLoginAuthentication,
Version,
get,
create,
update,
discard,
Any,
GetAny,
CreateAny,
UpdateAny,
DiscardAny,
} from "../src/api";
import { OAuthConfig } from "../src/oauth";
import { OAuthConfig } from "../src/auth";
//const directLogin: DirectLoginAuthentication = {
// username: process.env.OBP_USERNAME,
// password: process.env.OBP_PASSWORD,
// consumerKey: process.env.OBP_CONSUMER_KEY,
//};
const directLogin: DirectLoginAuthentication = {
username: process.env.OBP_USERNAME,
password: process.env.OBP_PASSWORD,
consumerKey: process.env.OBP_CONSUMER_KEY,
};
const oauthConfig: OAuthConfig = {
baseUri: "https://apisandbox.openbankproject.com",
consumerKey: "qhpewipvmnm3ivjk4eoanjauzac34hm0hlec3tct",
@ -28,17 +34,79 @@ const oauthConfig: OAuthConfig = {
const clientConfig: APIClientConfig = {
baseUri: "https://apisandbox.openbankproject.com",
version: Version.v510,
//authentication: directLogin,
oauthConfig: oauthConfig,
withFixedVersion: true,
authentication: directLogin,
//oauthConfig: oauthConfig,
};
(async () => {
// Get Resource Docs
//console.log(
// await get<API.Any>(clientConfig, Any)(GetAny)(
// "/resource-docs/v5.1.0/obp?tags=Account"
// )
//);
//// Get current user login
//console.log(await get<API.Any>(clientConfig, Any)(GetAny)("/users/current"));
// Create transaction
//const transaction = {
// description: "test transaction full data",
// to: {
// bank_id: "rbs",
// account_id: "9e6b2f45-a449-4e87-b772-e74cc9d42448",
// },
// value: {
// currency: "EUR",
// amount: 1.0,
// },
//};
//console.log(
// await create<API.Any>(clientConfig, Any)(CreateAny)(
// "banks/rbs/accounts/9e6b2f45-a449-4e87-b772-e74cc9d42448/owner/transaction-request-types/ACCOUNT/transaction-requests"
// )(transaction)
//);
// Create transaction attribute
//const transactionAttributeNew = {
// name: "HOUSE_RENT",
// type: "DATE_WITH_DAY",
// value: "123456789",
//};
//console.log(
// await create<API.Any>(clientConfig, Any)(CreateAny)(
// "banks/rbs/accounts/9e6b2f45-a449-4e87-b772-e74cc9d42448/transaction-requests/12f996cf-d3a9-4df3-8a1e-697a603609dd/attribute"
// )(transactionAttributeNew)
//);
// Update transaction
//const transactionAttributeUpdate = {
// name: "HOUSE_RENT",
// type: "DATE_WITH_DAY",
// value: "0000000",
//};
//console.log(
// await update<API.Any>(clientConfig, Any)(UpdateAny)(
// "banks/rbs/accounts/9e6b2f45-a449-4e87-b772-e74cc9d42448/transaction-requests/12f996cf-d3a9-4df3-8a1e-697a603609dd/attributes/49ff9d5f-3f7d-4b58-987a-de99cdbf2b39"
// )(transactionAttributeUpdate)
//);
// Create transaction attribute definition
//const transactionAttributeDefinition = {
// name: "SPECIAL_TAX_NUMBER",
// category: "TransactionRequest",
// type: "STRING",
// description: "description",
// alias: "STRING",
// can_be_seen_on_views: ["bank"],
// is_active: true,
//};
//console.log(
// await update<API.Any>(clientConfig, Any)(UpdateAny)(
// "banks/rbs/attribute-definitions/transaction-request"
// )(transactionAttributeDefinition)
//);
// Delete transaction attribute definition
console.log(
await get<API.Any>(clientConfig, Any)(GetAny)(
"/resource-docs/v5.1.0/obp?tags=Account"
await discard<API.Any>(clientConfig, Any)(DiscardAny)(
"banks/rbs/attribute-definitions/06122dff-8575-4ff1-9f42-427d3e44ac88/transaction-request"
)
);
// Get current user login
console.log(await get<API.Any>(clientConfig, Any)(GetAny)("/users/current"));
})();

View File

@ -3,7 +3,7 @@ import {
Version,
APIClientConfig,
DirectLoginAuthentication,
create,
post,
get,
Transaction,
} from "../src/api";
@ -52,7 +52,7 @@ const viewId = "owner";
amount: 1.0,
},
};
await create<API.Transaction>(
await post<API.Transaction>(
clientConfig,
Transaction
)(CreateTransactionRequestAccount)(

View File

@ -13,11 +13,9 @@ const jestConfig: JestConfigWithTsJest = {
obpUsername: process.env.OBP_USERNAME,
obpPassword: process.env.OBP_PASSWORD,
obpConsumerKey: process.env.OBP_CONSUMER_KEY,
//obpBaseUri: "https://apisandbox.openbankproject.com",
obpBaseUri: "https://obp-apisandbox.joinfincubator.com",
obpBaseUri: "https://apisandbox.openbankproject.com",
obpVersion: "v5.1.0",
//obpTestBank: "rbs",
obpTestBankId: "joinfincubator.01.uk.bk0",
obpTestBank: "obp1",
},
};

View File

@ -1,7 +1,7 @@
{
"name": "obp-typescript",
"version": "1.0.0",
"license": "MIT",
"version": "1.0.2",
"license": "Apache-2.0",
"scripts": {
"build": "tsc && tsc-alias",
"test": "jest --setupFiles dotenv/config",
@ -49,5 +49,27 @@
"__tests__/**/*.ts": [
"yarn lint"
]
}
},
"description": "## Usage",
"main": "index.js",
"directories": {
"doc": "docs",
"example": "examples"
},
"repository": {
"type": "git",
"url": "git+https://github.com/OpenBankProject/OBP-TypeScript.git"
},
"keywords": [
"typescript",
"open banking",
"obp",
"javascript",
"functional"
],
"author": "",
"bugs": {
"url": "https://github.com/OpenBankProject/OBP-TypeScript/issues"
},
"homepage": "https://github.com/OpenBankProject/OBP-TypeScript#readme"
}

View File

@ -28,6 +28,7 @@ import {
APIRequest,
APIClientConfig,
apiCallWithCustomURIPath,
apiCallWithCustomBody,
} from "./client";
/**
@ -51,6 +52,77 @@ export const GetAny =
return await methodCall(config, path);
};
/**
* Create Any Request.
*
* @param config - The APIClientConfig object
* @param methodCall - A higher order function
* @returns A curried function
*
* @see {@link APIClientConfig}
* @see {@link TransactionRequestAccountBody}
*
* @public
*/
export const CreateAny =
(
config: APIClientConfig,
methodCall: (
config: APIClientConfig,
path: string,
body: any
) => Promise<any>
) =>
(path: string) => {
return apiCallWithCustomBody<API.Any, any>(config, path, methodCall);
};
/**
* Update Any Request.
*
* @param config - The APIClientConfig object
* @param methodCall - A higher order function
* @returns A curried function
*
* @see {@link APIClientConfig}
* @see {@link TransactionRequestAccountBody}
*
* @public
*/
export const UpdateAny =
(
config: APIClientConfig,
methodCall: (
config: APIClientConfig,
path: string,
body: any
) => Promise<any>
) =>
(path: string) => {
return apiCallWithCustomBody<API.Any, any>(config, path, methodCall);
};
/**
* Delete Any Request.
*
* @param config - The APIClientConfig object
* @param methodCall - A higher order function
* @returns A curried function
*
* @see {@link APIClientConfig}
* @see {@link TransactionRequestAccountBody}
*
* @public
*/
export const DiscardAny =
(
config: APIClientConfig,
methodCall: (config: APIClientConfig, path: string) => Promise<any>
) =>
async (path: string) => {
return await methodCall(config, path);
};
/**
* Returns an anonymous function for creating or getting Any data.
*
@ -70,4 +142,30 @@ export const Any: APIRequest<API.Any> = {
) => {
return apiCallWithCustomURIPath<API.Any>(config, methodCall);
},
create: (
config: APIClientConfig,
methodCall: (
config: APIClientConfig,
path: string,
body: any
) => Promise<any>
) => {
return apiCallWithCustomURIPath<API.Any>(config, methodCall);
},
update: (
config: APIClientConfig,
methodCall: (
config: APIClientConfig,
path: string,
body: any
) => Promise<any>
) => {
return apiCallWithCustomURIPath<API.Any>(config, methodCall);
},
discard: (
config: APIClientConfig,
methodCall: (config: APIClientConfig, path: string) => Promise<any>
) => {
return apiCallWithCustomURIPath<API.Any>(config, methodCall);
},
};

View File

@ -24,7 +24,7 @@
*/
import superagent from "superagent";
import { OAuth, OAuthConfig } from "../oauth";
import { OAuth, OAuthConfig } from "../auth";
/**
* OBP API Versions.
@ -126,6 +126,8 @@ export type MethodCall<T> = (
export type APIRequest<T> = {
get?: (config: APIClientConfig, methodCall: MethodCall<T>) => any;
create?: (config: APIClientConfig, methodCall: MethodCall<T>) => any;
update?: (config: APIClientConfig, methodCall: MethodCall<T>) => any;
discard?: (config: APIClientConfig, methodCall: MethodCall<T>) => any;
};
/**
@ -254,7 +256,35 @@ const getDirectLoginToken = async (
};
/**
* Send a GET request and returns a response.
* Get the Oauth header.
*
* @param config - The APIClientConfig object
* @returns An {object} value
*
* @see APIClientConfig
*/
const getOauthHeader = async (
config: APIClientConfig,
path: string,
pathUri: string
): Promise<string> => {
let header: any;
if (config.oauthConfig) {
if (!config.oauthConfig.baseUri)
config.oauthConfig["baseUri"] = config.baseUri;
const oauth = new OAuth(config.oauthConfig);
header = oauth.authHeader(pathUri, "GET");
} else {
if (!config.token) {
config.token = await getDirectLoginToken(config);
header = config.token;
}
}
return header;
};
/**
* Send a GET HTTP request and returns a response.
*
* @param config - The APIClientConfig object
* @returns An {object} value
@ -268,25 +298,19 @@ export const getRequest = async (
path: string
): Promise<any> => {
const pathUri = uri(config, path);
let header: any;
if (config.oauthConfig) {
if (!config.oauthConfig.baseUri)
config.oauthConfig["baseUri"] = config.baseUri;
const oauth = new OAuth(config.oauthConfig);
header = oauth.authHeader(pathUri, "GET");
} else {
if (!config.token) {
config.token = await getDirectLoginToken(config);
header = config.token;
}
}
const header = await getOauthHeader(config, path, pathUri);
return JSON.parse(
(await superagent.get(pathUri).set("Authorization", header)).text
(
await superagent
.get(pathUri)
.set("Authorization", header)
.catch((error) => error.response)
).text
);
};
/**
* Send a POST request and returns a response.
* Send a POST HTTP request and returns a response.
*
* @param config - The APIClientConfig object
* @param path - The URI path
@ -303,15 +327,72 @@ export const postRequest = async (
body: any
): Promise<any> => {
const pathUri = uri(config, path);
if (!config.token) {
config.token = await getDirectLoginToken(config);
}
const header = await getOauthHeader(config, path, pathUri);
return JSON.parse(
(
await superagent
.post(pathUri)
.set("Authorization", config.token)
.set("Authorization", header)
.send(body)
.catch((error) => error.response)
).text
);
};
/**
* Send a PUT HTTP request and returns a response.
*
* @param config - The APIClientConfig object
* @param path - The URI path
* @param body - The request body
* @returns An {object} value
*
* @see APIClientConfig
*
* @public
*/
export const putRequest = async (
config: APIClientConfig,
path: string,
body: any
): Promise<any> => {
const pathUri = uri(config, path);
const header = await getOauthHeader(config, path, pathUri);
return JSON.parse(
(
await superagent
.put(pathUri)
.set("Authorization", header)
.send(body)
.catch((error) => error.response)
).text
);
};
/**
* Send a DELETE HTTP request and returns a response.
*
* @param config - The APIClientConfig object
* @param path - The URI path
* @param body - The request body
* @returns An {object} value
*
* @see APIClientConfig
*
* @public
*/
export const deleteRequest = async (
config: APIClientConfig,
path: string
): Promise<any> => {
const pathUri = uri(config, path);
const header = await getOauthHeader(config, path, pathUri);
return JSON.parse(
(
await superagent
.delete(pathUri)
.set("Authorization", header)
.catch((error) => error.response)
).text
);
};
@ -353,3 +434,41 @@ export const create = <T>(
): any => {
return request.create(config, postRequest);
};
/**
* A PUT request function that updates an API data and returns the result.
*
* @param config - The APIClientConfig object
* @param request - The APIRequest object
* @returns An @typeParam {Object} value
*
* @see APIClientConfig
* @see APIRequest<T>
*
* @public
*/
export const update = <T>(
config: APIClientConfig,
request: APIRequest<T>
): any => {
return request.update(config, putRequest);
};
/**
* A DELETE request function that deletes an API data.
*
* @param config - The APIClientConfig object
* @param request - The APIRequest object
* @returns An @typeParam {Object} value
*
* @see APIClientConfig
* @see APIRequest<T>
*
* @public
*/
export const discard = <T>(
config: APIClientConfig,
request: APIRequest<T>
): any => {
return request.discard(config, deleteRequest);
};

View File

@ -40,7 +40,7 @@ export {
CreateTransactionRequestAccount,
} from "./transaction";
export { User, Current } from "./user";
export { Any, GetAny } from "./any";
export { Any, GetAny, CreateAny, UpdateAny, DiscardAny } from "./any";
export {
API,
Version,
@ -50,4 +50,6 @@ export {
DirectLoginAuthentication,
get,
create,
update,
discard,
} from "./client";

View File

@ -79,14 +79,37 @@ export class OAuth {
);
}
/**
* Get oauth.OAuth instance.
*
* @returns An {oauth.OAuth} value
*
* @public
*/
get(): oauth.OAuth {
return this.instance;
}
/**
* Get OAuthConfig object.
*
* @returns An {OAuthConfig} value
*
* @public
*/
configs(): OAuthConfig {
return this.config;
}
/**
* Get the Oauth authentication header.
*
* @param pathUri - The the relative path of the URL.
* @param method - The http method.
* @returns An {OAuthConfig} value
*
* @public
*/
authHeader(pathUri: string, method: string): string {
if (!this.config.accessToken) console.warn("Access token is not set.");

View File

@ -40,7 +40,7 @@ export {
CreateTransactionRequestAccount,
} from "./api/transaction";
export { User, Current } from "./api/user";
export { Any, GetAny } from "./api/any";
export { Any, GetAny, CreateAny, UpdateAny, DiscardAny } from "./api/any";
export {
API,
Version,
@ -50,5 +50,7 @@ export {
DirectLoginAuthentication,
get,
create,
update,
discard,
} from "./api/client";
export { OAuth, OAuthConfig } from "./oauth";