{"id":17204679,"url":"https://github.com/aexol/syncano-middleware-common","last_synced_at":"2025-03-25T09:43:38.166Z","repository":{"id":57095417,"uuid":"122603657","full_name":"aexol/syncano-middleware-common","owner":"aexol","description":null,"archived":false,"fork":false,"pushed_at":"2018-04-05T14:46:53.000Z","size":163,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-01T21:25:09.355Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aexol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-23T09:55:47.000Z","updated_at":"2018-04-05T14:46:55.000Z","dependencies_parsed_at":"2022-08-22T23:10:29.009Z","dependency_job_id":null,"html_url":"https://github.com/aexol/syncano-middleware-common","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aexol%2Fsyncano-middleware-common","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aexol%2Fsyncano-middleware-common/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aexol%2Fsyncano-middleware-common/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aexol%2Fsyncano-middleware-common/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aexol","download_url":"https://codeload.github.com/aexol/syncano-middleware-common/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245437274,"owners_count":20615242,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T02:22:47.192Z","updated_at":"2025-03-25T09:43:38.148Z","avatar_url":"https://github.com/aexol.png","language":"TypeScript","readme":"# Syncano Middleware Common\n\n## About\nCollection of common utilities for sockets in syncano that can be used in conjunction with @aexol/syncano-middleware lib.\n\n## Installation\nnpm install @aexol/syncano-middleware-common\n\n## Usage\n\n## API Reference\n## Modules\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#module_@aexol/syncano-middleware-common\"\u003e@aexol/syncano-middleware-common\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eCommon middlewares for syncano.\u003c/p\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n## Functions\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#allowedMethods\"\u003eallowedMethods(fn, allowed)\u003c/a\u003e ⇒ \u003ccode\u003efunction\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eChecks if request is allowed based on request method.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#loggedIn\"\u003eloggedIn(fn, opts)\u003c/a\u003e ⇒ \u003ccode\u003eObject\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eChecks if user is logged in, returning response with 403 and message if not.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#parseGETFields\"\u003eparseGETFields(fn)\u003c/a\u003e ⇒ \u003ccode\u003efunction\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eParses args in GET request as json if possible. If not, leaves them unchanged.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#replaceBuffers\"\u003ereplaceBuffers(fn, opts)\u003c/a\u003e ⇒ \u003ccode\u003eObject\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReplace all buffers in socket args.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#rootAccount\"\u003erootAccount(fn, opts)\u003c/a\u003e ⇒ \u003ccode\u003eObject\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eRoot account check middleware.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#toBool\"\u003etoBool(fn, fields)\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eAttempts to cast certain fields in request to bool.\nCan be useful to handling both GET and POST input on endpoint\nas GET endpoints will always have a string.\u003c/p\u003e\n\u003cp\u003eFields that are either \u003ccode\u003etrue\u003c/code\u003e or \u003ccode\u003e\u0026#39;true\u0026#39;\u003c/code\u003e will evaluate to true.\nEverything else will be considered false.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#toNumber\"\u003etoNumber(fn, fields)\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eAttempts to cast certain fields in request to number.\nCan be useful to handling both GET and POST input on endpoint\nas GET endpoints will always have a string.\u003c/p\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n\u003ca name=\"module_@aexol/syncano-middleware-common\"\u003e\u003c/a\u003e\n\n## @aexol/syncano-middleware-common\nCommon middlewares for syncano.\n\n\u003ca name=\"allowedMethods\"\u003e\u003c/a\u003e\n\n## allowedMethods(fn, allowed) ⇒ \u003ccode\u003efunction\u003c/code\u003e\nChecks if request is allowed based on request method.\n\n**Kind**: global function  \n**Access**: public  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| fn | \u003ccode\u003efunction\u003c/code\u003e \\| \u003ccode\u003eObject\u003c/code\u003e | Either next function in chain or object with  `key: value` pairs of method and handler function for method type. |\n| allowed | \u003ccode\u003eArray\u003c/code\u003e | List of allowed methods in case of fn being function. Optional. |\n\n**Example**  \n```javascript\nimport serve, {response} from '@aexol/syncano-middleware'\nimport {allowedMethods} from '@aexol/syncano-middleware-common'\n\nasync function hello(ctx, syncano) {\n    return response.success({message: `Hello, ${ctx.meta.user.username}`)\n}\n\nexport default ctx =\u003e serve(ctx, allowedMethods(hello, ['GET']))\n```\n**Example**  \n```javascript\nimport serve, {response} from '@aexol/syncano-middleware'\nimport {allowedMethods} from '@aexol/syncano-middleware-common'\n\nasync function hello(ctx, syncano) {\n    return response.success({message: `Hello, ${ctx.meta.user.username}`)\n}\n\nexport default ctx =\u003e serve(ctx, allowedMethods({\n GET: hello,\n POST: hello\n}))\n```\n\u003ca name=\"loggedIn\"\u003e\u003c/a\u003e\n\n## loggedIn(fn, opts) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nChecks if user is logged in, returning response with 403 and message if not.\n\n**Kind**: global function  \n**Access**: public  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| fn | \u003ccode\u003efunction\u003c/code\u003e | Next function in request chain |\n| opts | \u003ccode\u003eObject\u003c/code\u003e | Additional options. Optional |\n| opts.message | \u003ccode\u003eString\u003c/code\u003e | Alternative message if user is not logged in. |\n\n**Example**  \n```javascript\nimport serve, {response} from '@aexol/syncano-middleware'\nimport {loggedIn} from '@aexol/syncano-middleware-common'\n\nasync function hello(ctx, syncano) {\n    return response.success({message: `Hello, ${ctx.meta.user.username}`)\n}\n\nexport default ctx =\u003e serve(ctx, loggedIn(hello))\n```\n\u003ca name=\"parseGETFields\"\u003e\u003c/a\u003e\n\n## parseGETFields(fn) ⇒ \u003ccode\u003efunction\u003c/code\u003e\nParses args in GET request as json if possible. If not, leaves them unchanged.\n\n**Kind**: global function  \n**Access**: public  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| fn | \u003ccode\u003efunction\u003c/code\u003e | Next function in request chain |\n\n**Example**  \n```javascript\nimport serve, {response} from '@aexol/syncano-middleware'\nimport {parseGETFields} from '@aexol/syncano-middleware-common'\n\nasync function hello(ctx, syncano) {\n    return response.success({message: `Hello, ${ctx.meta.user.username}`)\n}\n\nexport default ctx =\u003e serve(ctx, parseGETFields(hello))\n```\n\u003ca name=\"replaceBuffers\"\u003e\u003c/a\u003e\n\n## replaceBuffers(fn, opts) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nReplace all buffers in socket args.\n\n**Kind**: global function  \n**Access**: public  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| fn | \u003ccode\u003efunction\u003c/code\u003e | Handler function |\n| opts | \u003ccode\u003eObject\u003c/code\u003e | Aditional opts. Optional |\n| opts.replaceFn | \u003ccode\u003efunction\u003c/code\u003e | If set, will be used to replace buffer contents instead of default behaviour. |\n| opts.exclude | \u003ccode\u003eArray\u003c/code\u003e | List of args to skip from replacing. |\n| opts.encoding | \u003ccode\u003eString\u003c/code\u003e | Input encoding of buffer in args. |\n| opts.inputEncoding | \u003ccode\u003eString\u003c/code\u003e | Output encoding of buffer in args. Unless, replaceFn was set, this middleware replaces all buffers with it's string content in place. Modifies ctx.args. |\n\n**Example**  \n```javascript\nimport serve, {response} from '@aexol/syncano-middleware'\nimport {replaceBuffers} from '@aexol/syncano-middleware-common'\n\nasync function hello(ctx, syncano) {\n    return response.success({message: 'ok'})\n}\n\nexport default ctx =\u003e serve(ctx, replaceBuffers(hello))\n```\n\u003ca name=\"rootAccount\"\u003e\u003c/a\u003e\n\n## rootAccount(fn, opts) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nRoot account check middleware.\n\n**Kind**: global function  \n**Access**: public  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| fn | \u003ccode\u003efunction\u003c/code\u003e | Next function in chain. |\n| opts | \u003ccode\u003eObject\u003c/code\u003e | Additional options. Optional. |\n| opts.message | \u003ccode\u003eString\u003c/code\u003e | Custom error message if not root account. |\n| opts.condFn | \u003ccode\u003efunction\u003c/code\u003e | Check for root only if function evaluates to true. |\n\n**Example**  \n```javascript\nimport serve, {response} from '@aexol/syncano-middleware'\nimport {rootAccount} from '@aexol/syncano-middleware-common'\n\nasync function hello(ctx, syncano) {\n    return response.success({message: `Hello, ${ctx.meta.admin.email}`)\n}\n\nexport default ctx =\u003e serve(ctx, rootAccount(hello))\n```\n\u003ca name=\"toBool\"\u003e\u003c/a\u003e\n\n## toBool(fn, fields)\nAttempts to cast certain fields in request to bool.\nCan be useful to handling both GET and POST input on endpoint\nas GET endpoints will always have a string.\n\nFields that are either `true` or `'true'` will evaluate to true.\nEverything else will be considered false.\n\n**Kind**: global function  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| fn | \u003ccode\u003efunction\u003c/code\u003e | Next function in chain |\n| fields | \u003ccode\u003eArray\u003c/code\u003e | fields to cast to bool |\n\n\u003ca name=\"toNumber\"\u003e\u003c/a\u003e\n\n## toNumber(fn, fields)\nAttempts to cast certain fields in request to number.\nCan be useful to handling both GET and POST input on endpoint\nas GET endpoints will always have a string.\n\n**Kind**: global function  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| fn | \u003ccode\u003efunction\u003c/code\u003e | Next function in chain |\n| fields | \u003ccode\u003eArray\u003c/code\u003e | fields to cast to number |\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faexol%2Fsyncano-middleware-common","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faexol%2Fsyncano-middleware-common","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faexol%2Fsyncano-middleware-common/lists"}