Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert arrays in form data to overloaded key #33

Merged
merged 6 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 245 additions & 0 deletions bin/tsl-mastodon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
#!/usr/bin/env node
/*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*\

TypeScript Library for the Mastodon API

Copyright (c) TypeScriptLibs and Contributors

Licensed under the MIT License.
You may not use this file except in compliance with the License.
You can get a copy of the License at https://typescriptlibs.org/LICENSE.txt

\*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*/


/* *
*
* Imports
*
* */


import * as FS from 'node:fs/promises';

import * as Mastodon from '../lib/index.js';


/* *
*
* Constants
*
* */


const HELP = [
'tsl-mastodon [COMMAND] [OPTIONS]',
'',
'COMMANDS:',
' post Post a status in the timeline.',
'',
'OPTIONS:',
' --api [str] API address of the Mastodon server.',
' --api2 [str] API v2 address of the Mastodon server.',
' --help -h Show this help.',
' --image [str] Image path.',
' --text [str] Text string.',
' --token [hex] API token.',
' --version -v Show the version of tsl-mastodon.'
].join( '\n' );


/* *
*
* Functions
*
* */


/**
* @param {string} [commandKey]
* @return {Record<string,(boolean|string|Array<string>)>}
*/
function argv (
commandKey = ''
) {
const argv = {};

let lastKey = commandKey;
let lastValue;

for ( const arg of process.argv.slice( process.argv0 === 'node' ? 2 : 0 ) ) {

if ( arg[0] === '-' ) {

lastKey = arg.replace( /^-+/gu, '' );
lastValue = argv[lastKey];

if ( lastValue instanceof Array ) {
lastValue.push( true );
}
else if ( typeof lastValue !== 'undefined' ) {
argv[lastKey] = [lastValue, true];
}
else {
argv[lastKey] = true;
}

}
else {

lastValue = argv[lastKey];

if ( lastValue instanceof Array ) {
if ( typeof lastValue[lastValue.length - 1] === 'boolean' ) {
lastValue[lastValue.length - 1] = arg;
}
else {
lastValue.push( arg );
}
}
else if ( typeof lastValue === 'string' ) {
argv[lastKey] = [lastValue, arg];
}
else {
argv[lastKey] = arg;
}

}

}

return argv;
}


/**
* @param {object} obj
* @return {string}
*/
function json (
obj
) {
return JSON.stringify( obj, null, ' ' );
}


/**
* @param {Record<string,(boolean|string|Array<string>)>} args
* @param {Mastodon.API} api
* @param {Mastodon.API} api2
* @return {Promise<void>}
*/
async function post (
args,
api,
api2
) {
/** @type {Mastodon.JSON.StatusPost} */
let status;

if ( args.image ) {
const images = (
args.image instanceof Array ?
args.image :
[args.image]
);
/** @type {Mastodon.JSON.MediaStatusPost} */
const post = {
media_ids: []
};

for ( const image of images ) {
const file = await Mastodon.Utilities.fileFrom( '' + image );

console.log( file.name, file.size + ' bytes' );

let attachment = await api2.postMediaAttachment( {
file
} );

if ( !attachment.json ) {
throw attachment;
}

post.media_ids.push( attachment.json.id );
}

if ( args.text ) {
post.status = '' + args.text;
}

status = post;
}
else {
/** @type {Mastodon.JSON.TextStatusPost} */
const post = {
status: '' + args.text
};

status = post;
}

console.log( 'REQUEST:', json( status ) );
console.log( 'RESPONSE:', json( await api.postStatus( status ) ) );

}


async function main () {
const args = argv( 'command' );

if ( args.h || args.help ) {
console.log( HELP );
return;
}

if ( args.v || args.version ) {
console.log(
JSON.parse(
await FS.readFile(
new URL( '../package.json', import.meta.url )
.href.substring( 7 )
)
).version
);
return;
}

console.log( 'INPUT:', json( args ) );

const api = new Mastodon.API( {
access_token: '' + args.token,
api_url: '' + ( args.api || args.api2 )
} );
const api2 = new Mastodon.API( {
access_token: '' + args.token,
api_url: '' + ( args.api2 || args.api )
} );

switch ( args.command ) {
case 'post':
try {
post( args, api, api2 );
}
catch ( error ) {
console.error( 'ERROR:', json( error ) );
process.exit( 1 );
}

break;
default:
console.error( 'Unknown command' );
break;
}

}


/* *
*
* CLI
*
* */


main();
9 changes: 6 additions & 3 deletions lib/API.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

Copyright (c) TypeScriptLibs and Contributors

Licensed under the MIT License; you may not use this file except in
compliance with the License. You may obtain a copy of the MIT License at
https://typescriptlibs.org/LICENSE.txt
Licensed under the MIT License.
You may not use this file except in compliance with the License.
You can get a copy of the License at https://typescriptlibs.org/LICENSE.txt

\*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*/
import * as JSON from './JSON/index.js';
Expand Down Expand Up @@ -416,6 +416,9 @@ export declare namespace API {
with_dismissed?: boolean;
}
interface Config extends REST.Config {
/**
* API version to distinguish between multiple instances of the API.
*/
api_version?: number;
}
interface NotificationParams extends QueryParams {
Expand Down
7 changes: 3 additions & 4 deletions lib/API.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/API.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/Bridge.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

Copyright (c) TypeScriptLibs and Contributors

Licensed under the MIT License; you may not use this file except in
compliance with the License. You may obtain a copy of the MIT License at
https://typescriptlibs.org/LICENSE.txt
Licensed under the MIT License.
You may not use this file except in compliance with the License.
You can get a copy of the License at https://typescriptlibs.org/LICENSE.txt

\*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*/
export declare const Bridge: {
Expand Down
6 changes: 3 additions & 3 deletions lib/Bridge.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Bridge.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/OAuth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

Copyright (c) TypeScriptLibs and Contributors

Licensed under the MIT License; you may not use this file except in
compliance with the License. You may obtain a copy of the MIT License at
https://typescriptlibs.org/LICENSE.txt
Licensed under the MIT License.
You may not use this file except in compliance with the License.
You can get a copy of the License at https://typescriptlibs.org/LICENSE.txt

\*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*/
export declare namespace OAuth {
Expand Down
6 changes: 3 additions & 3 deletions lib/OAuth.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/OAuth.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/REST.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

Copyright (c) TypeScriptLibs and Contributors

Licensed under the MIT License; you may not use this file except in
compliance with the License. You may obtain a copy of the MIT License at
https://typescriptlibs.org/LICENSE.txt
Licensed under the MIT License.
You may not use this file except in compliance with the License.
You can get a copy of the License at https://typescriptlibs.org/LICENSE.txt

\*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*i*/
declare global {
Expand Down
10 changes: 6 additions & 4 deletions lib/REST.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading