Skip to content

Commit ce4de36

Browse files
authored
Merge pull request #68 from fa0311/dev
0.0.19 and add example
2 parents f92c58b + c1d34a8 commit ce4de36

File tree

11 files changed

+3264
-25
lines changed

11 files changed

+3264
-25
lines changed

example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

example/.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"singleQuote": true,
3+
"semi": true,
4+
"bracketSpacing": true,
5+
"arrowParens": "always",
6+
"printWidth": 120,
7+
"trailingComma": "all"
8+
}

example/package-lock.json

Lines changed: 3122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "example",
3+
"license": "MIT",
4+
"version": "1.0.0",
5+
"author": "",
6+
"description": "",
7+
"scripts": {
8+
"start": "npx ts-node -r tsconfig-paths/register src/main.ts"
9+
},
10+
"dependencies": {
11+
"twitter-api-v2": "^1.15.2",
12+
"twitter-openapi-typescript": "^0.0.19"
13+
},
14+
"devDependencies": {
15+
"@types/node": "*",
16+
"@types/node-fetch": "*",
17+
"@typescript-eslint/eslint-plugin": "*",
18+
"@typescript-eslint/parser": "*",
19+
"dotenv": "^16.3.0",
20+
"eslint": "^8.50.0",
21+
"eslint-config-standard-with-typescript": "*",
22+
"eslint-plugin-import": "*",
23+
"eslint-plugin-n": "*",
24+
"eslint-plugin-promise": "*",
25+
"log4js": "^6.9.0",
26+
"node-fetch": "^2.0.0",
27+
"typescript": "^5.2.0"
28+
}
29+
}

example/src/login.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { TwitterOpenApi } from 'twitter-openapi-typescript';
2+
import { TwitterApi, ITwitterApiClientPlugin } from 'twitter-api-v2';
3+
import fetch from 'node-fetch';
4+
5+
import * as dotenv from 'dotenv';
6+
dotenv.config();
7+
8+
const authToken = process.env.AUTH_TOKEN as string;
9+
const CsrfToken = process.env.CSRF_TOKEN as string;
10+
11+
export const login = async () => {
12+
const cookie = { auth_token: authToken, ct0: CsrfToken };
13+
14+
const api = new TwitterOpenApi({ fetchApi: fetch as any });
15+
const client = await api.getClientFromCookies(cookie);
16+
17+
const plugin: ITwitterApiClientPlugin = {
18+
onBeforeRequest: async (params) => {
19+
params.computedParams.headers = {
20+
...params.computedParams.headers,
21+
...TwitterOpenApi.api_key,
22+
'x-csrf-token': cookie.ct0,
23+
'x-twitter-auth-type': 'OAuth2Session',
24+
authorization: `Bearer ${TwitterOpenApi.bearer}`,
25+
cookie: api.cookieEncode(cookie),
26+
};
27+
params.requestOptions.headers = {
28+
...params.requestOptions.headers,
29+
...TwitterOpenApi.api_key,
30+
'x-csrf-token': cookie.ct0,
31+
'x-twitter-auth-type': 'OAuth2Session',
32+
authorization: `Bearer ${TwitterOpenApi.bearer}`,
33+
cookie: api.cookieEncode(cookie),
34+
};
35+
},
36+
};
37+
38+
const legacy = new TwitterApi('_', { plugins: [plugin] });
39+
40+
return { client, legacy };
41+
};
42+
43+
export default login;

example/src/tweetMedia.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import login from '@/login';
2+
3+
const main = async () => {
4+
const { client, legacy } = await login();
5+
6+
const data = await legacy.v1.uploadMedia('test.png');
7+
8+
client.getPostApi().postCreateTweet({
9+
tweetText: 'Hello World!!',
10+
mediaIds: [data],
11+
});
12+
};
13+
14+
main();

example/test.png

447 Bytes
Loading

example/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"target": "es5",
5+
"downlevelIteration": true,
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"outDir": "dist",
9+
"lib": ["es2020", "dom"],
10+
"typeRoots": ["node_modules/@types"],
11+
"allowSyntheticDefaultImports": true,
12+
"rootDir": "./",
13+
"baseUrl": "./",
14+
"paths": {
15+
"@/*": ["./src/*"]
16+
},
17+
"declarationMap": true,
18+
"sourceMap": true,
19+
"strict": true
20+
}
21+
}

twitter-openapi-typescript/package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

twitter-openapi-typescript/package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twitter-openapi-typescript",
3-
"version": "0.0.18",
3+
"version": "0.0.19",
44
"description": "Implementation of Twitter internal API in TypeScript",
55
"scripts": {
66
"test": "jest",
@@ -25,19 +25,19 @@
2525
"twitter-openapi-typescript-generated": "^0.0.12"
2626
},
2727
"devDependencies": {
28-
"@types/jest": "any",
29-
"@types/node": "any",
30-
"@types/node-fetch": "any",
31-
"@typescript-eslint/eslint-plugin": "any",
32-
"@typescript-eslint/parser": "any",
28+
"@types/jest": "*",
29+
"@types/node": "*",
30+
"@types/node-fetch": "*",
31+
"@typescript-eslint/eslint-plugin": "*",
32+
"@typescript-eslint/parser": "*",
3333
"dotenv": "^16.3.0",
3434
"eslint": "^8.50.0",
35-
"eslint-config-standard-with-typescript": "any",
36-
"eslint-plugin-import": "any",
37-
"eslint-plugin-n": "any",
38-
"eslint-plugin-promise": "any",
35+
"eslint-config-standard-with-typescript": "*",
36+
"eslint-plugin-import": "*",
37+
"eslint-plugin-n": "*",
38+
"eslint-plugin-promise": "*",
3939
"jest": "^29.7.0",
40-
"ts-jest": "any",
40+
"ts-jest": "*",
4141
"log4js": "^6.9.0",
4242
"node-fetch": "^2.0.0",
4343
"typescript": "^5.2.0"

0 commit comments

Comments
 (0)