本文整理汇总了TypeScript中strands.Strands.multiline方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Strands.multiline方法的具体用法?TypeScript Strands.multiline怎么用?TypeScript Strands.multiline使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类strands.Strands
的用法示例。
在下文中一共展示了Strands.multiline方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: toParamsFunction
private toParamsFunction(child: NestedResource, client: string, isChild: boolean, attach?: string) {
const className = `${this.currentPath.join('.')}.${uppercamelcase(child.methodName)}`;
const func2Return = new Strands();
const path = isChild ? 'this.path + ' : '';
func2Return.multiline(`(uriParams) => new ${className}(
${client},
${path}template(${javascriptStringify(child.relativeUri)},
Object.assign(${javascriptStringify(getDefaultParameters(child.uriParameters))}, uriParams))
),${attach}`);
return func2Return.toString();
}
示例2: camelCase
this.supportedSecuritySchemes.forEach((scheme: any, index: number, schemes: any[]) => {
const name = camelCase(scheme.name);
const trailing = index < schemes.length - 1 ? ',' : '';
if (scheme.type === 'OAuth 2.0') {
this.buffer.return();
this.buffer.line('// eslint-disable-next-line');
this.buffer.line(` ${name}: function ${name}(options) {`);
this.buffer.multiline(` const schemeSettings = ${this.formatJSON(scheme.settings, 2, 4)};`);
this.buffer.line(` return new ClientOAuth2(Object.assign(schemeSettings, options));`);
this.buffer.line(` }${trailing}`);
}
});
示例3: toParamsMethod
private toParamsMethod(child: NestedResource, client: string, isChild: boolean) {
const className = this.currentPath.concat([uppercamelcase(child.methodName)]).join('.');
const func2Return = new Strands();
const path = isChild ? 'this.path + ' : '';
func2Return.multiline(`(uriParams) {
return new ${className}(
${client},
${path}template(${javascriptStringify(child.relativeUri)},
Object.assign(${this.formatJSON(getDefaultParameters(child.uriParameters), 2, 8)}, uriParams))
);
}`);
return func2Return.toString();
}
示例4: generateRequiresAndGlobalFunctions
private generateRequiresAndGlobalFunctions() {
this.buffer.line(`/* eslint no-use-before-define: 0 */`);
if (hasSecurity(this.api, 'OAuth 2.0')) {
this.buffer.line(`const ClientOAuth2 = require('client-oauth2');`);
}
this.buffer.multiline(`const rp = require('request-promise');
const queryString = require('query-string');
const debuglog = require('util').debuglog('${this.api.title}');
const TEMPLATE_REGEXP = /\{\\+?([^\{\}]+)\}/g;
const template = (string, interpolate) => string.replace(TEMPLATE_REGEXP, (match, key) => {
if (interpolate[key] != null) {
return encodeURIComponent(interpolate[key]);
}
return '';
});
const request = (client, method, path, opts) => {
const headers = opts.headers ? Object.assign(
{}, client.options.headers, opts.headers
) : client.options.headers;
const options = Object.assign({}, client.options, opts);
const baseUri = template(options.baseUri, options.baseUriParameters);
if (typeof options.query === 'string') {
options.query = queryString.parse(options.query);
}
let reqOpts = {
url: baseUri.replace(/\\/$/, '') + template(path, options.uriParameters),
json: !Buffer.isBuffer(options.body),
method,
headers,
formData: options.formData,
body: options.body,
qs: options.query,
options: options.options,
resolveWithFullResponse: true
};
if (options.options !== undefined) {
reqOpts = Object.assign(reqOpts, options.options);
}
if (options.user && typeof options.user.sign === 'function') {
reqOpts = options.user.sign(reqOpts);
}
debuglog(\`[REQUEST]: \${JSON.stringify(reqOpts, null, 2)}\`);
return rp(reqOpts)
.then((response) => {
const responseLog = {
headers: response.headers,
body: response.body,
statusCode: response.statusCode
};
debuglog(\`[RESPONSE]: \${JSON.stringify(responseLog, null, 2)}\`);
// adding backward compatibility
response.status = response.statusCode;
return response;
})
.catch((error) => {
debuglog(\`[RESPONSE]: \${JSON.stringify(error, null, 2)}\`);
// rethrow the error so that the returned promise is rejected
throw error;
});
};`);
}
示例5: createClientClass
private createClientClass() {
this.buffer.multiline(`
class Client {
constructor(options) {
this.path = '';
this.options = Object.assign({
baseUri: ${javascriptStringify(this.api.baseUri)},
baseUriParameters: ${this.formatJSON(getDefaultParameters(this.api.baseUriParameters), 2, 6).split('\n').join('\n') },
headers: {}
}, options);
this.customRequest = (method, path, opts) => request(
this, method, path, opts
);\n
this.form = (payload) => {
const data = {
formData: payload,
append(key, value) {
if (typeof value !== 'string') {
this.formData.file = value;
} else {
data.formData[key] = value;
}
}
};
return data;
};\n`);
this.createThisResources(this.withParams, this.noParams, 'this', false);
this.buffer.line(` }\n`);
// Adding the extensibility point
this.buffer.multiline(` setHeaders(headers) {
this.options.headers = headers;`);
this.buffer.line(` }`);
this.buffer.multiline(`
use(name, module) {
const moduleType = typeof module;
if (Object.prototype.hasOwnProperty.call(this, name)) {
throw Error(\`The property \${name} already exists\`);
}
switch (moduleType) {
case 'string':
// eslint-disable-next-line
this[name] = require(module);
break;
case 'function':
this[name] = new module(); // eslint-disable-line new-cap
break;
case 'object':
this[name] = module;
break;
case 'undefined':
if (typeof name === 'string') {
// eslint-disable-next-line
this[name] = require(name);
break;
}
throw Error('Cannot create the extension point with the values provided');
default:
throw Error('Cannot create the extension point with the values provided');
}
}`);
for (const resource of this.flatTree) {
const { relativeUri, uriParameters } = resource;
for (const method of resource.methods) {
if (method.annotations && method.annotations['client.methodName']) {
const methodName = method.annotations['client.methodName'].structuredValue;
const type = isQueryMethod(method) ? 'query' : 'body';
const headers = getDefaultParameters(method.headers);
const headersText = Object.keys(headers).length === 0 ? '' : `, headers: ${javascriptStringify(headers)}`;
if (Object.keys(uriParameters).length) {
this.buffer.return();
this.buffer.line(` ${methodName}(uriParams, ${type}, opts) {`);
this.buffer.line(` const uriParameters = Object.assign(` +
`${javascriptStringify(getDefaultParameters(uriParameters))}, uriParams);`);
this.buffer.line(` const options = Object.assign({ ${type}, ` +
`uriParameters: uriParameters${headersText} }, opts);`);
this.buffer.line(` return request(this, ${javascriptStringify(method.method)}, ` +
`${javascriptStringify(relativeUri)}, options);`);
this.buffer.line(` }`);
} else {
this.buffer.return();
this.buffer.line(` ${methodName}(${type}, opts) {`);
this.buffer.line(` const options = Object.assign({ ${type}${headersText} }, opts);`);
this.buffer.line(` return request(this, ${javascriptStringify(method.method)}, ` +
`${javascriptStringify(relativeUri)}, options);`);
this.buffer.line(` }`);
}
}
}
}
this.createProtoMethods(this.nestedTree.methods, 'this', `''`);
this.createProtoResources(this.withParams, this.noParams);
this.buffer.line(`}`);
}
示例6: function
export default function (api: Api): string {
const s = new Strands()
const flatTree = allResources(api) // For short-hand annotations.
const nestedTree = nestedResources(api)
const { withParams, noParams } = separateChildren(nestedTree)
const supportedSecuritySchemes = getSecuritySchemes(api).filter(x => x.type === 'OAuth 2.0')
if (hasSecurity(api, 'OAuth 2.0')) {
s.line(`var ClientOAuth2 = require('client-oauth2')`)
}
s.multiline(`var popsicle = require('popsicle')
var extend = require('xtend')
var setprototypeof = require('setprototypeof')
var TEMPLATE_REGEXP = /\\{([^\\{\\}]+)\\}/g
module.exports = Client
function template (string, interpolate) {
return string.replace(TEMPLATE_REGEXP, function (match, key) {
if (interpolate[key] != null) {
return encodeURIComponent(interpolate[key])
}
return ''
})
}
function request (client, method, path, opts) {
var options = extend({}, client._options, opts)
var baseUri = template(options.baseUri, options.baseUriParameters)
var reqOpts = {
url: baseUri.replace(/\\/$/, '') + template(path, options.uriParameters),
method: method,
headers: options.headers,
body: options.body,
query: options.query,
options: options.options
}
if (options.user && typeof options.user.sign === 'function') {
reqOpts = options.user.sign(reqOpts)
}
return popsicle.request(reqOpts)
}
function Client (options) {
this._path = ''
this._options = extend({
baseUri: ${stringify(api.baseUri)},
baseUriParameters: ${stringify(getDefaultParameters(api.baseUriParameters)) }
}, options)
function client (method, path, options) {
return request(client, method, path, options)
}
`)
createThisResources(withParams, noParams, 'client', '')
s.line(` setprototypeof(client, this)`)
s.line(` return client`)
s.line(`}`)
s.line()
s.line(`Client.form = popsicle.form`)
s.line(`Client.version = ${stringify(api.version)}`)
s.line('Client.Security = {')
supportedSecuritySchemes.forEach((scheme: any, index: number, schemes: any[]) => {
const name = camelCase(scheme.name)
const trailing = index < schemes.length ? ',' : ''
if (scheme.type === 'OAuth 2.0') {
s.line(` ${name}: function (options) { return new ClientOAuth2(extend(${stringify(scheme.settings)}, options)) }${trailing}`)
}
})
s.line('}')
for (const resource of flatTree) {
const { relativeUri, uriParameters } = resource
for (const method of resource.methods) {
if (method.annotations && method.annotations['client.methodName']) {
const methodName = method.annotations['client.methodName'].structuredValue
const type = isQueryMethod(method) ? 'query' : 'body'
const headers = getDefaultParameters(method.headers)
if (Object.keys(uriParameters).length) {
s.line(`Client.prototype.${methodName} = function (uriParams, ${type}, opts) {`)
s.line(` var uriParameters = extend(${stringify(getDefaultParameters(uriParameters))}, uriParams)`)
s.line(` var options = extend({ ${type}: ${type}, uriParameters: uriParameters, headers: ${stringify(headers)} }, opts)`)
s.line(` return request(this, ${stringify(method.method)}, ${stringify(relativeUri)}, options)`)
s.line(`}`)
} else {
s.line(`Client.prototype.${methodName} = function (${type}, opts) {`)
//.........这里部分代码省略.........
示例7: function
export default function (api: Api) {
const s = new Strands()
const projectName = paramCase(api.title)
const className = pascalCase(api.title)
s.multiline(`# ${api.title}
> Browser and node module for making API requests against [${api.title}](${api.baseUri}).
## Installation
\`\`\`sh
npm install ${projectName} --save
\`\`\`
## Usage
\`\`\`js
var ${className} = require('${projectName}')
var client = new ${className}()
\`\`\`
`)
if (hasSecurity(api, 'OAuth 2.0')) {
s.multiline(`### Authentication
#### OAuth 2.0
This API supports authentication with [OAuth 2.0](https://github.com/mulesoft/js-client-oauth2). Initialize the \`OAuth2\` instance with the application client id, client secret and a redirect uri to authenticate with users.
\`\`\`js
var auth = new ${className}.security.<method>({
clientId: '123',
clientSecret: 'abc',
redirectUri: 'http://example.com/auth/callback'
});
// Available methods for OAuth 2.0:`)
for (const scheme of getSecuritySchemes(api)) {
if (scheme.type === 'OAuth 2.0') {
s.line(` - ${camelCase(scheme.name)}`)
}
}
s.line('```')
}
s.multiline(`### Options
You can set options when you initialize a client or at any time with the \`options\` property. You may also override options per request by passing an object as the last argument of request methods. For example:
\`\`\`javascript
var client = new ${className}({ ... })
client('GET', '/', {
baseUri: 'http://example.com',
headers: {
'Content-Type': 'application/json'
}
})
\`\`\`
#### Base URI
You can override the base URI by setting the \`baseUri\` property, or initializing a client with a base URI. For example:
\`\`\`javascript
new ${className}({
baseUri: 'https://example.com'
});
\`\`\`
### Helpers
Exports \`${className}.form\`, which exposes a cross-platform \`FormData\` interface that can be used with request bodies.
### Methods
All methods return a HTTP request instance of [Popsicle](https://github.com/blakeembrey/popsicle), which allows the use of promises (and streaming in node).
`)
for (const resource of allResources(api)) {
for (const method of resource.methods) {
s.line(`#### ${getDisplayName(method, resource)}`)
s.line()
if (Object.keys(resource.uriParameters).length) {
s.line(getUriParametersSnippet(resource))
s.line()
}
if (method.description) {
s.multiline(method.description.trim())
s.line()
}
s.multiline(`\`\`\`js
client.${getRequestSnippet(method, resource)}.then(...)
//.........这里部分代码省略.........