Cloudflare Worker template script error - country redirect - redirect

I grabbed example script from the Cloudflare GitHub worker example page for a simple country redirect. It fails and I don't know why? (I am not particularly strong in code stuff).
Error is:
Script modified; context reset. worker.js:17 Uncaught TypeError: Cannot read properties of undefined (reading 'country')
at redirect (worker.js:17)
at worker.js:28 redirect # worker.js:17 (anonymous) # worker.js:28 Uncaught (in response) TypeError: Cannot read properties of undefined (reading 'country')
The template code is:
/**
* A map of the URLs to redirect to
* #param {Object} countryMap
*/
const countryMap = {
US: "https://example.com/us",
EU: "https://eu.example.com/",
}
/**
* Returns a redirect determined by the country code
* #param {Request} request
*/
function redirect(request) {
// Use the cf object to obtain the country of the request
// more on the cf object: https://developers.cloudflare.com/workers/runtime-apis/request#incomingrequestcfproperties
const country = request.cf.country
if (country != null && country in countryMap) {
const url = countryMap[country]
return Response.redirect(url)
} else {
return fetch(request)
}
}
addEventListener("fetch", event => {
event.respondWith(redirect(event.request))
})
From
https://github.com/cloudflare/cloudflare-docs/blob/production/products/workers/src/content/examples/country-code-redirect.md
Does anyone have advice on this? This is being run on the Cloudflare Workers system.

This is a known problem with the Worokers preview. request.cf is always undefined when using the embedded preview next to the editor. If you use wrangler dev to test your code instead, it will work correctly there, and it will also work once deployed.

Related

check if api endpoint exits before send request - axios

I have axios request in my vue application:
async get_banner(id:number) : Promise<any> {
return global.axios.get(`${process.env.VUE_APP_DOMAIN}/banners/${id}`)
}
it works while banner/${id} response exits, but I have situation when I should disable banner in my admin panel so api endpoint becomes empty. (not exits) so I am getting Request failed with status code 404 in my vue app console.
question is how to prevent error and know if url exits or not? what is best practice to do this?
You can't tell whether an API exists or not without trying it (or relying on another API to get status of the former API)
It's usually just a manner of handling the response properly. Usually this would look something like this...
getTheBanner(id){
this.errorMessage = null; // reset message on request init
get_banner(id)
.then(r => {
// handle success
this.results = r;
})
.catch(e => {
// handle error
if (e.status === 404){
// set error message
this.errorMessage = "Invalid banner Id";
}
})
}
then in your template you could have something like this
<div v-if="errorMessage" class="alert danger">{errorMessage}</div>
Explaination:
Yes, you're absolutely right. This is the default behavior of strapi. Whenever the response is empty it throws a 404 error. This is basically because the findOne method in service returns null to the controller and when the controller sends this to the boom module it returns a 404 Not Found error to the front end.
Solution:
Just override the find one method in the controller to return an empty object {} when the response is null.
Implementation
// Path - yourproject/api/banner/controllers/banner.js
const { sanitizeEntity } = require('strapi-utils');
module.exports = {
/**
* Retrieve a record.
*
* #return {Object}
*/
async findOne(ctx) {
const { id } = ctx.params;
const entity = await strapi.services.restaurant.findOne({ id });
// in case no entity is found, just return emtpy object here.
if(!entity) return {};
return sanitizeEntity(entity, { model: strapi.models.restaurant });
},
};
Side Note:
There's no need to make any changes to the browser side axios implementation. You should always handle such cases in controller rather the client side.
Reference:
Backend Customizations

Dingo Api response->created | location and content example

I am creating API with Laravel 5.2 and Dingo API package. When a user is created, I want to return 201 response with the new $user->id.
My Code
return $this->response->created();
As per Dingo documentatio, I can provide a location and $content as parameters in the created() function.
My question is, what location information I need to return here and I tried to set my new user as $content, but it's not working or I am not sure what to expect.
Can someone please explain this created() function?
What this does is set the Location header, as seen in the source:
/**
* Respond with a created response and associate a location if provided.
*
* #param null|string $location
*
* #return \Dingo\Api\Http\Response
*/
public function created($location = null, $content = null)
{
$response = new Response($content);
$response->setStatusCode(201);
if (! is_null($location)) {
$response->header('Location', $location);
}
return $response;
}
So, in your example since you're creating a new user, you might send the users profile page as the location, something like:
return $this->response->created('/users/123');
As for the content, as you can see in the function this sets the content on the return. In your case, it would probably be a json string with the new user information, something like:
return $this->response->created('/users/123', $user); // laravel should automatically json_encode the user object

ZF2 JSON-RPC - Error response, data parameter, how to access?

Let's say I have an RPC call for adding colors. A user can only add colors once. If they add one a second time, I want to return an error response that tells them they screwed up, and why.
The JSON-RPC error response describes an error object that includes a space for a data parameter. It is in here that it seems appropriate to include my error code for "color already added". However, I cannot figure out how to return this in the response.
$jsonRpc = new Server();
$jsonRpc->setClass(new Testi());
$jsonRpc->getRequest()->setVersion(Server::VERSION_2);
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
echo $jsonRpc->getServiceMap()->setEnvelope(Smd::ENV_JSONRPC_2);
}
else {
$jsonRpc->handle();
}
class Testi {
const ERROR_CODE_COLOR_EXISTS = 5;
private $colors = ['red', 'green'];
/**
* #param $color
* #return array
*/
public function addColor($color) {
if (in_array($color, $this->colors)) {
throw new \Exception('Color exists');
}
else {
$this->colors[] = $color;
}
return $this->colors;
}
}
This works, to the degree that an error response is returned, but gives me no option to include my error code (self::ERROR_CODE_COLOR_EXISTS).
{"error":{"code":-32000,"message":"Color exists","data":{}},"id":"","jsonrpc":"2.0"}
How do I put info into that DATA parameter!?
Thanks,
Adam
Turns out you have two choices to do this:
1) Add parameters to an exception:
$e = new Exception('I pooped my pants');
$e->color = 'brown';
$->smell = 'bad';
color and smell would then be in the data parameter of the error response.
2) Pass the server (in my code, $jsonRpc) into the object (in my code, it would look something like: new Testi($jsonRpc)), and use the fault(...) method, which allows you to pass the data array/object into it.
The latter approach gives you more flexibility as you can do data->code, data->message, neither of which can you set on the $e object, because they are existing, protected parameters. But you are then coupling your model to the $jsonRpc server, which is not good.
All that being said, it's not the correct way to respond to the scenario I outlined above. The error response is more or less reserved for true, unrecoverable server errors, akin to real exceptions, not user validation errors. In my case it was better to define a response type that allows me to return success/fail values with appropriate response codes. {success: false, code: 5}.
Cheers,
Adam

Exchanging Facebook Auth Code for Access Token using the PHP SDK

I am trying to build a server-to-server auth flow using the Facebook PHP SDK and no Javascript, as outlined here. So far, I have successfully created a LoginUrl that lets the User sign in with Facebook, then redirect back to my App and check the state parameter for CSFR protection.
My Problem is, that I can't seem to get the API-call working that should swap my Auth Code for an access token. I pillaged every similar problem anyone else that Google was able to find had encountered for possible solutions.
Yet the end result was always the same: no access token, no error message that I could evaluate.
Researching the topic yielded the following advice, which I tested:
The URL specified in the App Settings must be a parent folder of $appUrl.
use curl to make the request instead of the SDK function api()
I've been at this for 2 days straight now and really could use some help.
<?php
require '../inc/php-sdk/src/facebook.php';
// Setting some config vars
$appId = 'MY_APP_ID';
$secret = 'MY_APP_SECRET';
$appUrl = 'https://MY_DOMAIN/appFolder';
$fbconfig = array('appId'=>$appId, 'secret'=>$secret);
$facebook = new Facebook($fbconfig);
// Log User in with Facebook and come back with Auth Code if not yet done
if(!(isset($_SESSION['login']))){
$_SESSION['login']=1;
header('Location: '.$facebook->getLoginUrl());
}
// process Callback from Facebook User Login
if($_SESSION['login']===1) {
/* CSFR Protection: getLoginUrl() generates a state string and stores it
in "$_SESSION['fb_'.$fbconfig['appId'].'_state']". This checks if it matches the state
obtained via $_GET['state']*/
if (isset($_SESSION['fb_'.$fbconfig['appId'].'_state'])&&isset($_GET['state'])){
// Good Case
if ($_SESSION['fb_'.$fbconfig['appId'].'_state']===$_GET['state']) {
$_SESSION['login']=2;
}
else {
unset($_SESSION['login']);
echo 'You may be a victim of CSFR Attacks. Try logging in again.';
}
}
}
// State check O.K., swap Code for Token now
if($_SESSION['login']===2) {
$path = '/oauth/access_token';
$api_params = array (
'client_id'=>$appId,
'redirect_uri'=>$appUrl,
'client_secret'=>$secret,
'code'=>$_GET['code']
);
$access_token = $facebook->api($path, 'GET', $api_params);
var_dump($access_token);
}
The easiest way I found to do this is to extend the Facebook class and expose the protected getAccessTokenFromCode() method:
<?php
class MyFacebook extends Facebook {
/** If you simply want to get the token, use this method */
public function getAccessTokenFromCode($code, $redirectUri = null)
{
return parent::getAccessTokenFromCode($code, $redirectUri);
}
/** If you would like to get and set (and extend), use this method instead */
public function setAccessTokenFromCode($code)
{
$token = parent::getAccessTokenFromCode($code);
if (empty($token)) {
return false;
}
$this->setAccessToken($token);
if (!$this->setExtendedAccessToken()) {
return false;
}
return $this->getAccessToken();
}
}
I also included a variation on the convenience method I use to set the access token, since I don't actually need a public "get" method in my own code.

Doctrine Mongo ODM merge for externally modified data

I'm writing a Symfony2 app that allows mobile users to create and update "Homes" via a REST service. I'm using MongoDB as the storage layer and Doctrine MongoDB ODM to do the Document handling.
The GET /homes/{key} and POST /homes methods are working fine. The problem comes when I attempt to update an existing Home with PUT /homes/{key}.
Here's the current code:
/**
* PUT /homes/{key}
*
* Updates an existing Home.
*
* #param Request $request
* #param string $key
* #return Response
* #throws HttpException
*/
public function putHomeAction(Request $request, $key)
{
// check that the home exists
$home = $this->getRepository()->findOneBy(array('key' => (int) $key));
// disallow create via PUT as we want to generate key ourselves
if (!$home) {
throw new HttpException(403, 'Home key: '.$key." doesn't exist, to create use POST /homes");
}
// create object graph from JSON string
$updatedHome = $this->get('serializer')->deserialize(
$request->getContent(), 'Acme\ApiBundle\Document\Home', 'json'
);
// replace existing Home with new data
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$home = $dm->merge($updatedHome);
$dm->flush();
$view = View::create()
->setStatusCode(200)
->setData($home);
$response = $this->get('fos_rest.view_handler')->handle($view);
$response->setETag(md5($response->getContent()));
$response->setLastModified($home->getUpdated());
return $response;
}
The JSON string passed to the action is successfully deserialized to my Document object graph by JMSSerializer, but when I attempt the merge & flush, I get the error:
Notice: Undefined index: in ..../vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php line 1265
I've been attempting to follow the documentation here: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/working-with-objects.html#merging-documents
Is there something I need to do to the deserialized Home before attempting to merge it? Is merge the wrong method?
Thanks.
The only way I found to do this is to create a method in your document class that takes the updated document (e.g. $updatedHome) as a parameter and then just copies the required fields over into the existing document (e.g. $home).
So above, the code:
// replace existing Home with new data
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$home = $dm->merge($updatedHome);
$dm->flush();
can be replaced with:
// replace existing Home with new data
$home->copyFromSibling($updatedHome);
$this->getDocumentManager()->flush();
and then it will work.