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

move [Twitter] url to query param #4027

Merged
merged 6 commits into from
Sep 16, 2019
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
18 changes: 8 additions & 10 deletions services/suggest.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ describe('GitHub badge suggestions', function() {
link:
'https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2Fatom%2Fatom',
example: {
pattern: '/twitter/url/:protocol(https|http)/:hostAndPath+',
namedParams: {
protocol: 'https',
hostAndPath: 'github.com/atom/atom',
pattern: '/twitter/url',
namedParams: {},
queryParams: {
url: 'https://github.com/atom/atom',
},
queryParams: {},
},
preview: {
style: 'social',
Expand Down Expand Up @@ -173,12 +172,11 @@ describe('GitHub badge suggestions', function() {
link:
'https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2Fbadges%2Fnot-a-real-project',
example: {
pattern: '/twitter/url/:protocol(https|http)/:hostAndPath+',
namedParams: {
protocol: 'https',
hostAndPath: 'github.com/badges/not-a-real-project',
pattern: '/twitter/url',
namedParams: {},
queryParams: {
url: 'https://github.com/badges/not-a-real-project',
},
queryParams: {},
},
preview: {
style: 'social',
Expand Down
6 changes: 3 additions & 3 deletions services/suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function twitterPage(url) {
url.href
)}`,
example: {
pattern: '/twitter/url/:protocol(https|http)/:hostAndPath+',
namedParams: { protocol: `${schema}`, hostAndPath: `${host}${path}` },
queryParams: {},
pattern: '/twitter/url',
namedParams: {},
queryParams: { url: `${schema}://${host}${path}` },
},
preview: {
style: 'social',
Expand Down
9 changes: 4 additions & 5 deletions services/suggest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,11 @@ describe('Badge suggestions', function() {
link:
'https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2Fatom%2Fatom',
example: {
pattern: '/twitter/url/:protocol(https|http)/:hostAndPath+',
namedParams: {
protocol: 'https',
hostAndPath: 'github.com/atom/atom',
pattern: '/twitter/url',
namedParams: {},
queryParams: {
url: 'https://github.com/atom/atom',
},
queryParams: {},
},
preview: {
style: 'social',
Expand Down
19 changes: 19 additions & 0 deletions services/twitter/twitter-redirect.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const { redirector } = require('..')

module.exports = [
redirector({
category: 'social',
name: 'TwitterUrlRedirect',
route: {
base: 'twitter/url',
pattern: ':protocol(https|http)/:hostAndPath+',
},
transformPath: () => `/twitter/url`,
transformQueryParams: ({ protocol, hostAndPath }) => ({
url: `${protocol}://${hostAndPath}`,
}),
dateAdded: new Date('2019-09-17'),
}),
]
19 changes: 19 additions & 0 deletions services/twitter/twitter-redirect.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const { ServiceTester } = require('../tester')

const t = (module.exports = new ServiceTester({
id: 'TwitterUrlRedirect',
title: 'TwitterUrlRedirect',
pathPrefix: '/twitter/url',
}))

t.create('twitter')
.get('/https/shields.io.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/twitter/url.svg?url=${encodeURIComponent('https://shields.io')}`
)
22 changes: 13 additions & 9 deletions services/twitter/twitter.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@

const Joi = require('@hapi/joi')
const { metric } = require('../text-formatters')
const { optionalUrl } = require('../validators')
const { BaseService, BaseJsonService, NotFound } = require('..')

const queryParamSchema = Joi.object({
url: optionalUrl.required(),
}).required()

class TwitterUrl extends BaseService {
static get category() {
return 'social'
}

static get route() {
return {
base: 'twitter/url',
// Do not base new services on this route pattern.
// See https://github.com/badges/shields/issues/3714
pattern: ':protocol(https|http)/:hostAndPath+',
base: 'twitter',
pattern: 'url',
queryParamSchema,
}
}

static get examples() {
return [
{
title: 'Twitter URL',
namedParams: {
protocol: 'http',
hostAndPath: 'shields.io',
namedParams: {},
queryParams: {
url: 'https://shields.io',
},
// hard code the static preview
// because link[] is not allowed in examples
Expand All @@ -43,8 +47,8 @@ class TwitterUrl extends BaseService {
}
}

async handle({ protocol, hostAndPath }) {
const page = encodeURIComponent(`${protocol}://${hostAndPath}`)
async handle(_routeParams, { url }) {
const page = encodeURIComponent(`${url}`)
return {
label: 'tweet',
message: '',
Expand Down
2 changes: 1 addition & 1 deletion services/twitter/twitter.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ t.create('Invalid Username Specified (only spaces)')
})

t.create('URL')
.get('/url/https/shields.io.json')
.get('/url.json?url=https://shields.io')
.expectBadge({
label: 'tweet',
message: '',
Expand Down