-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
298 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
{ | ||
"name": "@midday/providers", | ||
"version": "1.0.0", | ||
"main": "src/index.ts", | ||
"private": true, | ||
"sideEffects": false, | ||
"scripts": { | ||
"clean": "rm -rf .turbo node_modules", | ||
"lint": "biome check .", | ||
"format": "biome --write .", | ||
"check:types": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@midday/kv": "workspace:*", | ||
"change-case": "^5.4.3" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.3.3" | ||
} | ||
"name": "@midday/providers", | ||
"version": "1.0.0", | ||
"main": "src/index.ts", | ||
"private": true, | ||
"sideEffects": false, | ||
"scripts": { | ||
"clean": "rm -rf .turbo node_modules", | ||
"lint": "biome check .", | ||
"format": "biome --write .", | ||
"check:types": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@midday/kv": "workspace:*", | ||
"axios": "^1.6.7", | ||
"change-case": "^5.4.3" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { client } from "@midday/kv"; | ||
import type { AxiosInstance, AxiosRequestConfig } from "axios"; | ||
import axios from "axios"; | ||
import { capitalCase } from "change-case"; | ||
import type { | ||
AuthenticatedRequest, | ||
GetAccountsResponse, | ||
GetTransactionsRequest, | ||
GetTransactionsResponse, | ||
} from "./types"; | ||
|
||
const ONE_HOUR = 3600; | ||
const ACCESS_VALID_FOR_DAYS = 180; | ||
const MAX_HISTORICAL_DAYS = 730; | ||
|
||
const keys = { | ||
accessToken: "go_cardless_access_token_v2", | ||
refreshToken: "go_cardless_refresh_token_v2", | ||
banks: "go_cardless_banks", | ||
}; | ||
|
||
export class GoCardLessApi { | ||
private api: AxiosInstance | null = null; | ||
private baseURL = "https://bankaccountdata.gocardless.com"; | ||
|
||
/** | ||
* List accounts a user granted access to in Teller Connect | ||
* | ||
* https://teller.io/docs/api/accounts | ||
*/ | ||
|
||
async getAccounts({ | ||
accessToken, | ||
}: AuthenticatedRequest): Promise<GetAccountsResponse> { | ||
const accounts = await this.get<GetAccountsResponse>( | ||
"/accounts", | ||
accessToken | ||
); | ||
|
||
return accounts; | ||
} | ||
|
||
/** | ||
* Get transactions for a single account | ||
* | ||
* https://teller.io/docs/api/transactions | ||
*/ | ||
|
||
async getTransactions({ | ||
accountId, | ||
accessToken, | ||
}: GetTransactionsRequest): Promise<GetTransactionsResponse> { | ||
return this.get<GetTransactionsResponse>( | ||
`/accounts/${accountId}/transactions`, | ||
accessToken | ||
); | ||
} | ||
|
||
private async getApi(accessToken: string): Promise<AxiosInstance> { | ||
if (!this.api) { | ||
this.api = axios.create({ | ||
baseURL: this.baseURL, | ||
timeout: 30_000, | ||
headers: { | ||
Accept: "application/json", | ||
}, | ||
auth: { | ||
username: accessToken, | ||
password: "", | ||
}, | ||
}); | ||
} else if (this.api.defaults.auth?.username !== accessToken) { | ||
this.api.defaults.auth = { | ||
username: accessToken, | ||
password: "", | ||
}; | ||
} | ||
|
||
return this.api; | ||
} | ||
|
||
/** Generic API GET request method */ | ||
private async get<TResponse>( | ||
path: string, | ||
accessToken: string, | ||
params?: any, | ||
config?: AxiosRequestConfig | ||
): Promise<TResponse> { | ||
const api = await this.getApi(accessToken); | ||
return api | ||
.get<TResponse>(path, { params, ...config }) | ||
.then(({ data }) => data); | ||
} | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export interface IETL<TInput, TExtracted = any, TTransformed = TExtracted> { | ||
extract(input: TInput): Promise<TExtracted>; | ||
transform(input: TInput, extracted: TExtracted): Promise<TTransformed>; | ||
load(input: TInput, transformed: TTransformed): Promise<void>; | ||
} | ||
|
||
export async function etl<TInput, TExtracted, TTransformed>( | ||
service: IETL<TInput, TExtracted, TTransformed>, | ||
input: TInput | ||
): Promise<void> { | ||
const extracted = await service.extract(input); | ||
const transformed = await service.transform(input, extracted); | ||
await service.load(input, transformed); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.